473,241 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 3097
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leon Jollans | last post by:
Hi. I'm reading a 5 meg (or so) file from the response stream of a WebRequest, and it's pitifully slow. I mean *really* slow, in the order of 15 minutes to download. Now obviously I'm caching this...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
5
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the...
8
by: Scott | last post by:
Hi guys, If I try to call read(), readline(), readtoend() and there is nothing to read (from a never ending loop for example) the program seems to continue but it exits the loop for no apparent...
6
by: Rob Meade | last post by:
Hi all, I'm having a few difficulties with the above, ie I cant find any good examples anywhere of how to do what I want to do! I have an xml file on my desktop which I want to read in. ...
7
by: .... | last post by:
Hi I have an existing function which has a stream object (inmsg.BodyPart.Data). I'm trying to search and replace the stream object in the most efficient way possible This is my attempt below,...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
3
by: Martin Z | last post by:
Hi, I have an application that involves sending a lot of XML data to various places. The problem is that once in a while, I just want the XML document as a string (for example, sending to a...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
20
by: tomPee | last post by:
Hi, I've bumbed into a slight problem now, and I just don't seem to know how to fix it. What I want to do is the following: Send over a socket: 1. Number of files to be send (not as an integer,...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.