Re: StreamReader cutting off first character
From: Matt Michael (ihate_at_spamforme.com)
Date: 08/27/04
- Next message: CJ Taylor: "Re: Generate a new guid"
- Previous message: SQLScott: "Re: StreamReader cutting off first character"
- In reply to: SQLScott: "Re: StreamReader cutting off first character"
- Next in thread: Herfried K. Wagner [MVP]: "Re: StreamReader cutting off first character"
- Messages sorted by: [ date ] [ thread ]
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
>>
>>
>>
- Next message: CJ Taylor: "Re: Generate a new guid"
- Previous message: SQLScott: "Re: StreamReader cutting off first character"
- In reply to: SQLScott: "Re: StreamReader cutting off first character"
- Next in thread: Herfried K. Wagner [MVP]: "Re: StreamReader cutting off first character"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|