471,082 Members | 926 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

stream reader

I use streamReader to read files from hard drive. Some files which I read
have spaces at the end of line. However whatever code I use
StreamReader1 = System.IO.File.OpenText(file)
or
StreamReader1 = New System.IO.StreamReader(file)
and then
StreamReader1.ReadToEnd()
or
StreamReader1.ReadLine()
I am losing spaces in the response. What I need to change to read all data.
Aleks Kleyn

Mar 18 '07 #1
12 3012
Aleks Kleyn wrote:
I use streamReader to read files from hard drive. Some files which I
read have spaces at the end of line. However whatever code I use
StreamReader1 = System.IO.File.OpenText(file)
or
StreamReader1 = New System.IO.StreamReader(file)
and then
StreamReader1.ReadToEnd()
or
StreamReader1.ReadLine()
I am losing spaces in the response. What I need to change to read all data.
Aleks Kleyn
Are you sure that you are losing the spaces when reading the text from
the file? Have you verified that the file is actually containing those
spaces, and that they really are space characters?

It makes no sense that anything would be removed when reading a file.
What do you do with the text after reading it, and how do you determine
that there are spaces missing?

--
Göran Andersson
_____
http://www.guffa.com
Mar 19 '07 #2
"Aleks Kleyn" <Al*********@MailAps.orgschrieb:
>I use streamReader to read files from hard drive. Some files which I read
have spaces at the end of line. However whatever code I use
StreamReader1 = System.IO.File.OpenText(file)
or
StreamReader1 = New System.IO.StreamReader(file)
and then
StreamReader1.ReadToEnd()
or
StreamReader1.ReadLine()
I am losing spaces in the response. What I need to change to read all
data.

How do you take a look at the data read from the file?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 19 '07 #3
It is very simple. I know place where line of file has spaces in the end
(for instance "ba ". Then I put break point at place where I read the
file, copy the value of variable into notepad and see that after charecter
'a' next line follows imediately. Discovered I this because stream was
procede not way as I expected.

Aleks Kleyn

Mar 19 '07 #4
"Aleks Kleyn" <Al*********@MailAps.orgwrote in message
news:LS************@newsfe12.lga...
It is very simple. I know place where line of file has spaces in the end
(for instance "ba ". Then I put break point at place where I read the
file, copy the value of variable into notepad and see that after charecter
'a' next line follows imediately. Discovered I this because stream was
procede not way as I expected.
I would expect this is a feature of the streamreader. Try opening the file
using a filestream instead.
>
Aleks Kleyn

Mar 19 '07 #5
On Mar 19, 12:42 am, "Michael C" <nos...@nospam.comwrote:
"Aleks Kleyn" <Aleks_Kl...@MailAps.orgwrote in message

news:LS************@newsfe12.lga...
It is very simple. I know place where line of file has spaces in the end
(for instance "ba ". Then I put break point at place where I read the
file, copy the value of variable into notepad and see that after charecter
'a' next line follows imediately. Discovered I this because stream was
procede not way as I expected.

I would expect this is a feature of the streamreader. Try opening the file
using a filestream instead.
Aleks Kleyn
No, something else must be happening here. I use StreamReader all the
time to read text files and spaces at the ends of the line are always
read.

Are the spaces at the end of the line followed by both a carriage
return and linefeed character?

Can you show us a simple program that duplicates the error.

Chris

Mar 19 '07 #6
Aleks Kleyn wrote:
It is very simple. I know place where line of file has spaces in the end
(for instance "ba ". Then I put break point at place where I read the
file, copy the value of variable into notepad and see that after
charecter 'a' next line follows imediately. Discovered I this because
stream was procede not way as I expected.

Aleks Kleyn
Either the spaces aren't there from the start, or you have to do
something that removes them.

I tried this code:

Dim r As StreamReader
r = File.OpenText("d:\temp\test.txt")
s = r.ReadToEnd
r.Dispose()

With the file test.txt containing four lines:
====================
asdf
b
asdfasdf
qwerty
====================

I put a breakpoint after the code and looked at the contents of the s
variable, and the spaces are there.

--
Göran Andersson
_____
http://www.guffa.com
Mar 19 '07 #7
I think it has carriage
return and linefeed character, because i edited it by ms studio, and it
warns if something is absent.
..
The code is
Dim TextStream As System.IO.StreamReader
If (My.Computer.FileSystem.FileExists(fpath)) Then
TextStream=new System.IO.StreamReader(fpath,True)
'TextStream = System.IO.File.OpenText(fpath)
HtmString = TextStream.ReadToEnd()
TextStream.Close()
Both cases, comented and uncomented give the same answer.

Mar 20 '07 #8
Aleks Kleyn wrote:
I think it has carriage
return and linefeed character, because i edited it by ms studio, and it
warns if something is absent.
Then it's most likely that program that removes the spaces when you save
the file.

Open the file in Notepad and check if the spaces are there. It's a plain
text editor without any syntax formatting features, so it won't remove
characters for you.

--
Gran Andersson
_____
http://www.guffa.com
Mar 20 '07 #9
Yesterday after your email I thought that may be problem that I use
asp.net code and you visual basic. I repeat this code in visual basic
and got the same result (by the way I do not see spaces in your email,
at least they disapear in response windows; may be this server has the
same problem). Today I tried in VB next code

Dim HtmString As String
Dim TextStream As System.IO.StreamReader
Dim fso As Object, TextStream1 As Object
If (My.Computer.FileSystem.FileExists(fpath)) Then
TextStream = New System.IO.StreamReader(fpath, True)
'TextStream = System.IO.File.OpenText(fpath)
HtmString = System.IO.File.ReadAllText(fpath)
HtmString = TextStream.ReadToEnd()
TextStream.Close()
fso = CreateObject("Scripting.FileSystemObject")
TextStream1 = fso.OpenTextFile(fpath)
HtmString1 = TextStream1.ReadAll()
End If

HtmString and HtmString1 were different and HtmString1 has all spaces.
Aleks Kleyn

Mar 20 '07 #10
Al*********@MailAPS.org wrote:
Yesterday after your email I thought that may be problem that I use
asp.net code and you visual basic.
What programming language are you using then? ASP.NET is not a
programming language, just a platform. I assumed that you were using
VB.NET as you are posting in the VB.NET newsgroup.
I repeat this code in visual basic
and got the same result (by the way I do not see spaces in your email,
at least they disapear in response windows; may be this server has the
same problem).
You are right, the spaces disappeared from the post. Each line in the
file was padded with spaces to eight characters in length.
Today I tried in VB next code

Dim HtmString As String
Dim TextStream As System.IO.StreamReader
Dim fso As Object, TextStream1 As Object
If (My.Computer.FileSystem.FileExists(fpath)) Then
TextStream = New System.IO.StreamReader(fpath, True)
'TextStream = System.IO.File.OpenText(fpath)
HtmString = System.IO.File.ReadAllText(fpath)
HtmString = TextStream.ReadToEnd()
TextStream.Close()
fso = CreateObject("Scripting.FileSystemObject")
TextStream1 = fso.OpenTextFile(fpath)
HtmString1 = TextStream1.ReadAll()
End If

HtmString and HtmString1 were different and HtmString1 has all spaces.
Aleks Kleyn
The equivalent of the FileSystemObject code would be:

HtmlString = System.IO.ReadAllText(path, System.Text.Encoding.ASCII)

--
Gran Andersson
_____
http://www.guffa.com
Mar 20 '07 #11
On Mar 20, 2:44 pm, Gran Andersson <g...@guffa.comwrote:
Aleks_Kl...@MailAPS.org wrote:
Yesterday after your email I thought that may be problem that I use
asp.net code and you visual basic.

What programming language are you using then? ASP.NET is not a
programming language, just a platform. I assumed that you were using
VB.NET as you are posting in the VB.NET newsgroup.
I repeat this code in visual basic
and got the same result (by the way I do not see spaces in your email,
at least they disapear in response windows; may be this server has the
same problem).

You are right, the spaces disappeared from the post. Each line in the
file was padded with spaces to eight characters in length.


Today I tried in VB next code
Dim HtmString As String
Dim TextStream As System.IO.StreamReader
Dim fso As Object, TextStream1 As Object
If (My.Computer.FileSystem.FileExists(fpath)) Then
TextStream = New System.IO.StreamReader(fpath, True)
'TextStream = System.IO.File.OpenText(fpath)
HtmString = System.IO.File.ReadAllText(fpath)
HtmString = TextStream.ReadToEnd()
TextStream.Close()
fso = CreateObject("Scripting.FileSystemObject")
TextStream1 = fso.OpenTextFile(fpath)
HtmString1 = TextStream1.ReadAll()
End If
HtmString and HtmString1 were different and HtmString1 has all spaces.
Aleks Kleyn

The equivalent of the FileSystemObject code would be:

HtmlString = System.IO.ReadAllText(path, System.Text.Encoding.ASCII)

--
Gran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -
i using visual basic.net. There is no difference where I use it: in
asp or desktop (the only difference that there is no global vars like
we use them in desktop). Today's test showed this one more time.
Spaces do not disappear for reason of posting because all code runs on
the server.
You probably mean System.IO.File.ReadAllText, because IO does not have
such method.
Adding your encoding turned disappeared spaces to question mark, so
they are not really spaces. The question now is to find good encoding,
but this is no problem. It is interesting, because file was prepared
by ms word.
Thank you very much

Mar 20 '07 #12
Al*********@MailAPS.org wrote:
On Mar 20, 2:44 pm, Gran Andersson <g...@guffa.comwrote:
>Aleks_Kl...@MailAPS.org wrote:
>>Yesterday after your email I thought that may be problem that I use
asp.net code and you visual basic.
What programming language are you using then? ASP.NET is not a
programming language, just a platform. I assumed that you were using
VB.NET as you are posting in the VB.NET newsgroup.
>>I repeat this code in visual basic
and got the same result (by the way I do not see spaces in your email,
at least they disapear in response windows; may be this server has the
same problem).
You are right, the spaces disappeared from the post. Each line in the
file was padded with spaces to eight characters in length.


>>Today I tried in VB next code
Dim HtmString As String
Dim TextStream As System.IO.StreamReader
Dim fso As Object, TextStream1 As Object
If (My.Computer.FileSystem.FileExists(fpath)) Then
TextStream = New System.IO.StreamReader(fpath, True)
'TextStream = System.IO.File.OpenText(fpath)
HtmString = System.IO.File.ReadAllText(fpath)
HtmString = TextStream.ReadToEnd()
TextStream.Close()
fso = CreateObject("Scripting.FileSystemObject")
TextStream1 = fso.OpenTextFile(fpath)
HtmString1 = TextStream1.ReadAll()
End If
HtmString and HtmString1 were different and HtmString1 has all spaces.
Aleks Kleyn
The equivalent of the FileSystemObject code would be:

HtmlString = System.IO.ReadAllText(path, System.Text.Encoding.ASCII)

--
Gran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

i using visual basic.net. There is no difference where I use it: in
asp or desktop (the only difference that there is no global vars like
we use them in desktop).
Actually global variables doesn't exist at all in VB.NET. :) The
difference is that you can put variables in the form class in a windows
application, and as long as the form is shown the variables remain. In a
web application you have a page class instead, and that only lasts the
time that it takes to create one page, then it's gone.
Today's test showed this one more time.
Spaces do not disappear for reason of posting because all code runs on
the server.
I was talking about the spaces in my file that I was posting in the
newsgroup.
You probably mean System.IO.File.ReadAllText, because IO does not have
such method.
Yes, that's right.
Adding your encoding turned disappeared spaces to question mark, so
they are not really spaces. The question now is to find good encoding,
but this is no problem.
You can change the file extension to .bin and open it in Visual Studio.
That will show you a hex dump representation of the file, where you can
see exactly what it contains.

--
Gran Andersson
_____
http://www.guffa.com
Mar 20 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Leon Jollans | last post: by
6 posts views Thread by Yechezkal Gutfreund | last post: by
6 posts views Thread by Rob Meade | last post: by
7 posts views Thread by .... | last post: by
10 posts views Thread by John Kraft | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.