When reading a text file, it only reads some of them
I am trying to read a large text file (4000+ lines) and output each line to the console. I am using the following code
using (var reader = new StreamReader("list1.txt"))
{
while (!reader.EndOfStream)
Console.WriteLine(reader.ReadLine().Trim());
}
Console.Read();
This reads lines, but the problem is the read starts at line 4113, when should it start at the correct line? So this means that I only get 100 lines from the text file. Why is this happening?
Thank.
+3
source to share
2 answers
You may well find that only the last lines are displayed in the output window N
, where N
- about 100. In other words, the scroll buffer is smaller than you think.
You can check this by changing your code to something like:
int numlines = 0;
using (var reader = new StreamReader("list1.txt")) {
while (!reader.EndOfStream) {
Console.WriteLine(reader.ReadLine().Trim());
numlines++;
}
}
Console.WriteLine("Wrote " + numlines + " lines.");
Console.Read();
which will output the number of lines processed at the end.
You can change the height of the console with a command like:
Console.BufferHeight = 30000;
+2
source to share