stream reader

  • Thread starter Thread starter Aleks Kleyn
  • Start date Start date
A

Aleks Kleyn

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
 
Aleks said:
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?
 
Aleks Kleyn said:
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?
 
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
 
Aleks Kleyn said:
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.
 
I would expect this is a feature of the streamreader. Try opening the file
using a filestream instead.

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
 
Aleks said:
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.
 
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.
 
Aleks said:
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.
 
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
 
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)
 
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.


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








The equivalent of the FileSystemObject code would be:

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

--
Göran 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
 
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.
 
Back
Top