473,406 Members | 2,816 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,406 software developers and data experts.

I/O not reading the last line of a file.

Hello,

I'm trying to input a txt file. Each line in the text file contains a
discrete code or value -- so I want to populate the an arraylist with
each line. I've noticed a problem. The code I have never reads the
last line of the file. Here's the code:

while (!sr.EndOfStream)
{
line = sr.ReadLine();
}
sr.Close();

If the txt file has 10 lines, after this code, there are only 9 items
in the arraylist. How can I get that last line in my arraylist?
Suggestions are greatly appreciated.

Thanks!
Sep 1 '08 #1
8 1476
On Sun, 31 Aug 2008 17:31:56 -0700, Adam Sandler <co****@excite.comwrote:
I'm trying to input a txt file. Each line in the text file contains a
discrete code or value -- so I want to populate the an arraylist with
each line. I've noticed a problem. The code I have never reads the
last line of the file. Here's the code:

while (!sr.EndOfStream)
{
line = sr.ReadLine();
}
sr.Close();
That's because the EndOfStream property returns information about the
underlying stream, not the reader state itself. The reader buffers the
input data, and so very well may reach the end of the underlying stream
before it's returned all of the text in the stream. Clearly, that's
what's happening to you.
If the txt file has 10 lines, after this code, there are only 9 items
in the arraylist. How can I get that last line in my arraylist?
Instead of checking the EndOfStream property, just read until you get a
null line:

while ((line = sr.ReadLine()) != null)
{
}

Pete
Sep 1 '08 #2
I might be wrong, but looking at Red Gate's .NET Reflector [still
getting used to that...] EndOfStream appears to respect the buffer...?

Marc
Sep 1 '08 #3
On Mon, 01 Sep 2008 00:01:44 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
I might be wrong, but looking at Red Gate's .NET Reflector [still
getting used to that...] EndOfStream appears to respect the buffer...?
On my computer, using Roeder's Reflection, I see the same thing as you.
If there's data left in the buffer, or refilling the buffer returns a
non-zero number of characters, EndOfStream returns "false". But...

What version of .NET? I'm up-to-date on this computer. Maybe it didn't
used to and the OP is using an earlier version of .NET. The docs
certainly don't guarantee that it does, and in fact seem to me to imply
the opposite. On top of that, it not respecting the buffer is the best
explanation I can come up with for the OP seeing the behavior he's
describing.

I admit, I didn't go over to his office and look over his shoulder to
verify that his code is doing what he says it's doing. But it seems
reasonable to take his word for it. :)

To the OP: if you are using the latest version of .NET, then you should
post a concise-but-complete code sample that demonstrates the problem
because, as Marc points out, the code you posted so far should work fine.

Pete
Sep 1 '08 #4
Peter Duniho wrote:
On Mon, 01 Sep 2008 00:01:44 -0700, Marc Gravell
<ma**********@gmail.comwrote:
>I might be wrong, but looking at Red Gate's .NET Reflector [still
getting used to that...] EndOfStream appears to respect the buffer...?

On my computer, using Roeder's Reflection,
Same software. Lutz Roeder just gave/sold Reflector to Red Gate.

Arne
Sep 2 '08 #5
On Mon, 01 Sep 2008 19:30:48 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
Peter Duniho wrote:
>On Mon, 01 Sep 2008 00:01:44 -0700, Marc Gravell
<ma**********@gmail.comwrote:
>>I might be wrong, but looking at Red Gate's .NET Reflector [still
getting used to that...] EndOfStream appears to respect the buffer..?
On my computer, using Roeder's Reflection,

Same software. Lutz Roeder just gave/sold Reflector to Red Gate.
Well...okay. Fine. But do you know why the OP isn't getting the last
line of input?
Sep 2 '08 #6
Peter Duniho wrote:
On Mon, 01 Sep 2008 19:30:48 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>Peter Duniho wrote:
>>On Mon, 01 Sep 2008 00:01:44 -0700, Marc Gravell
<ma**********@gmail.comwrote:
I might be wrong, but looking at Red Gate's .NET Reflector [still
getting used to that...] EndOfStream appears to respect the buffer..?
On my computer, using Roeder's Reflection,

Same software. Lutz Roeder just gave/sold Reflector to Red Gate.

Well...okay. Fine. But do you know why the OP isn't getting the last
line of input?
Nope.

For that part I am like the rest - it would be nice to see a
complete example.

Arne
Sep 2 '08 #7
On Aug 31, 6:45*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 31 Aug 2008 17:31:56 -0700, Adam Sandler <cor...@excite.comwrote:
I'm trying to input a txt file. *Each line in the text file contains a
discrete code or value -- so I want to populate the an arraylist with
each line. *I've noticed a problem. *The code I have never reads the
last line of the file. *Here's the code:
* * * * * * while (!sr.EndOfStream)
* * * * * * {
* * * * * * * * line = sr.ReadLine();
* * * * * * }
* * * * * * sr.Close();

That's because the EndOfStream property returns information about the *
underlying stream, not the reader state itself. *The reader buffers the*
input data, and so very well may reach the end of the underlying stream *
before it's returned all of the text in the stream. *Clearly, that's *
what's happening to you.
If the txt file has 10 lines, after this code, there are only 9 items
in the arraylist. *How can I get that last line in my arraylist?

Instead of checking the EndOfStream property, just read until you get a *
null line:

* * *while ((line = sr.ReadLine()) != null)
* * *{
* * *}

Pete
Thanks for the help; that worked!
Sep 2 '08 #8
Thanks for the help; that worked!- Hide quoted text -

Interesting... can you perhaps tell us which version (including SP) of
the .NET framework you are using? It *looks* like (in 3.5 SP1, at
least) the two should work the same. I'm glad it is sorted though - a
good call by Pete.

Marc
Sep 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
16
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
0
by: philip20060308 | last post by:
Hi all, Has anyone ever seen Python 2.4.1's httplib choke when reading chunked content? I'm using it via urrlib2, and I ran into a particular server that returns something that httplib doesn't...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
3
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.