Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamReader.ReadLine restarts file read 1

Status
Not open for further replies.

shakespeare5677

Programmer
Jul 18, 2005
284
US
I'm reading a file line by line using the StreamReader's ReadLine. After reading 13 lines of variable lengths, I read 4 lines of 130 characters. Attempting to read the next line, also 130 characters, returns the file's 1st line. Am I doing something wrong?
-Max
 
Can you post your code up here for us to examine?

Thanks.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
The code is simple:

Code:
StreamReader sreader = new FileInfo(sourceFile).OpenText();
string infoLine;
for (int i=0; i<13; i++)
{
     infoLine = sreader.ReadLine();
}

infoLine = sreader.ReadLine(); // PRODUCES 1st line ??
infoLine = sreader.ReadLine(); // PRODUCES 2nd line ??

Thanks for the help.
-Max
 
Here is the file I'm trying to read:

Code:
                                                         ABCDEFG HIGKLMN INDEX
  
                                                    MINUTE BY MINUTE ANALYSIS REPORT
                                                                   FOR 
                                                  NEW TESTING ON TUESDAY MAY 09, 2000
  
       PREPARED FOR:                                           ABCDEFGHIJ
  
    MINUTE
   TESTING TESTING TESTING1234
     VALUES
      SHARE
   ------------------------------------------------------------------------------------------------------------------------        
     9:55P   9:56P   9:57P   9:58P   9:59P  10:00P  10:01P  10:02P  10:03P  10:04P  10:05P  10:06P  10:07P  10:08P  10:09P         
   ------------------------------------------------------------------------------------------------------------------------        
   TEST  01 XXX                                                                                                                    
     11.56    9.98    9.11   10.44    9.90   10.75   11.09   11.41   11.32    9.04    9.06    9.56    9.86   10.11    9.91         
                                                                                                                                   
   TSET  10 AAA                                                                                                                    
      4.96    5.26    5.28    8.84    9.11   10.39   11.65   12.66   13.72   13.65   14.61   14.92   14.26   12.89   11.80
 
YOu've limited yourself to 13 lines in your for loop (i<13), possibly better to do
Code:
     while (sr.Peek() >= 0) 
     {
          infoLine = sreader.ReadLine();
     }
to ensure you reach the end of the file.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
*note : sr = sreader

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
The 13 line limit is for testing purposes. Reading a line after 13'th line retrieves the first line in the file. To make it easier for anyone to test and stop it after the good lines, I looped it for 13 times.
-Max
 
You aren't using the right code to get the next line. By calling the method again, you are starting the process over.

You need to call it like this
Code:
using (StreamReader sr = new StreamReader("TestFile.txt")) 
{
   String line;
   // Read and display lines from the file until the end of 
   // the file is reached.
   while ((line = sr.ReadLine()) != null) 
   {
        // Do anything here
        Console.WriteLine(line);
   }
}

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
That's exactly what is being done 13times in the loop, and twice outside of it. The two calls outside the loop have been singled out as those indicative of the problem.
-Max
 
For the record, here are my results when I run the code that I've listed above.

Both lines appear in my output.

myresults4rg.gif


----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Is what was output in my command line not what you want?

I was merely stating that everytime you call ReadLine(), you are starting over. You only call it one time (in a loop) for each file that you want to read.

From what I understand, the code that I posted will do what you have asked, since I was able to output the second 130 char line.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Sorry, Guru7777, I had to skip a couple of days. You have to define what you mean by "starting over". If it's starting over on the next line, that's the expected result, of course, if you mean starting over from the beginning of the file, then that's even crazier than my original question. Looking at it now, I think you meant starting over on the next line.

The reason I was so confused is the fact that I was doing the exact same thing you suggested and still coming up with the problem.

Now, I believe I'm getting close to the real issue. I used the position property of the stream reader's base class hoping to change the position of the cursor earlier in the code. As it turns out that didn't work for some reason. The only result of the action was to produce the error I'm encountering, it seems. I guess I'll have to find a better way read from the beginning of the file earlier in code.

Thanks.

-Max
 
This is a "known" issue with StreamReader. It uses buffered input, and unless you specify otherwise when you construct a StreamReader, that buffer size is 1024. The .Position property of the .Base stream will refer to how many "buffers" have been read. This has NOTHING TO DO with how many "records" have been processed by the StreamReader itself. In other words, StreamReader may be doing it's ReadLine against the current buffer, or it may request a new buffer to be read in. It retrieves records/lines from the buffer, not from the file on disk.

It's a problem, one I've banged my head against more than once. You either need to code a solution that doesn't require re-positioning, or you need to use a binary reader and implement your own buffer-handling routines.

Or, you could try to maintain your own "position" property by calculating how many bytes each .ReadLine consumes, taking line-termination characters into account.

Thomas D. Greer
 
Now that answres my question. I was hoping it was something of the sort and I wasn't just crazy.
Thanks Thomas.
-Max
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top