Re: StreamReader cutting off first character

From: Matt Michael (ihate_at_spamforme.com)
Date: 08/27/04


Date: Fri, 27 Aug 2004 12:06:14 -0400

Scott, I'm not sure why it would continue to read, even after the end of
the file has been reached. sr.Peek looks at the next character in the file
stream, and when there are no more characters in the stream it returns -1.
If you would have a blank file, the loop would not execute, because the
condition is checked prior to entering the loop. Upon trying to enter the
last iteration of your loop, sr.Peek should be -1, and break out. Have you
closed your file stream with a sr.Close()?

-Matt

"SQLScott" <SQLScott@discussions.microsoft.com> wrote in message
news:A3FD89AD-D0B0-48C9-856E-13D26E00D3D9@microsoft.com...
> Perfect! Thanks! However, while that worked, the "Do Until" statement
> does
> not. Even after it reads the last line, it keeps reading the file and
> will
> continue to read until the cows come home. I have to manually "kill" the
> process.
>
> Meaning, when it doesn't stop reading the file when it reaches the last
> line. It is like the Energizer bunny, it keeps reading, and reading, and
> reading....
>
> My Do While statement looks like this:
>
> Do Until sr.Peek = -1
> tText = sr.readline
> Loop
>
> Thoughts?
>
> TIA!
>
> "Matt Michael" wrote:
>
>> Scott,
>>
>> Perhaps the problem is that you are using sr.read instead of sr.Peek. I
>> think that sr.Read is getting the first character in each line, then
>> readline reads until the end of that line, whereupon the process repeats
>> itself. Try doing something like this:
>>
>> Dim sr as StreamReader
>> sr = New steramreader(application.startuppath & "\file.txt")
>>
>> Do until sr.Peek = -1
>> tText = sr.ReadLine
>> Next
>>
>> Hope this helps
>>
>>
>> -Matt
>>
>> "SQLScott" <SQLScott@discussions.microsoft.com> wrote in message
>> news:F84CD882-2896-48A3-B001-F4D74C1674A1@microsoft.com...
>> >I have searched the groups for an answer to this question but have not
>> >found
>> > one that answers this specific question.
>> >
>> > I am using the StreamReader to read in from a text file. It works
>> > great
>> > EXCEPT for the fact that it cuts off the first character of each line.
>> >
>> > The code is simple enough:
>> >
>> > dim sr as streamreader
>> > sr = new streamreader(application.startuppath & "\file.txt")
>> >
>> > do while sr.read
>> > tText = sr.readline
>> > next
>> >
>> > tText now contains, for example, the text "his is an example" instead
>> > of
>> > "This is an example".
>> >
>> > Ideas? TIA!
>> >
>> > --
>> > Thanks,
>> >
>> > Scott
>>
>>
>>



Relevant Pages

  • Re: ReadLine weirdness!
    ... I changed my code to use Read and loop through the line ... reading one character at a time instead of the whole line with Readline and ... > instinct is to suspect your drive or file system. ... >>support this character yet I can see the line as it should be if I open it ...
    (microsoft.public.access.modulesdaovba)
  • Re: Close reads the rest of the stream? (HTTP) Another really bad CF bug?
    ... In the checked change event of your UI thread set a variable in a singleton ... class that your loop checks. ... I have a while look reading bytes from an ... stream before closing! ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: TCP/IP Sockets with GNAT.Sockets
    ... > end loop; ... Is your code reading character by character? ... some metadata to the stream. ... differentiate data from metadata. ...
    (comp.lang.ada)
  • Re: Use of nested loops.
    ... and processing each character in each line. ... >>line, reading each line from each file, and processing each character ... in many cases you use multiple nested loops without even knowing ... You need a loop in which to read a line, ...
    (comp.lang.c)
  • Re: Use of nested loops.
    ... and processing each character in each line. ... > triple-nested loop to open each of the files named on the command ... > line, reading each line from each file, and processing each character ... in many cases you use multiple nested loops without even knowing ...
    (comp.lang.c)

Loading