I have an file text with approximate 113687 lines, but my application reads only 314 lines, can anyone say why?
My code:
string file = @"z:\foo.txt";
StreamReader reader = File.OpenText(file);
string line;
int rows = 0;
while ((line = reader.ReadLine()) != null) {
++rows;
doSomethingWith(line);
// ...
}
The DoSomethingWith
function is similar to:
protected static bool DoSomethingWith(string line)
{
return Regex.Match(line, @"\d+\-\d+\.\d+\.\d+\.\d+").Success;
}
Updated:
In answer to Gregs question:
Does your foo.txt contain a Ctrl+Z character on line 314?
Yes, my file contains a Control-Z
character on line 314.
Text files on Windows can be terminated with a Ctrl+Z character. This means that when the file is read, the StreamReader
returns end-of-file when the Ctrl+Z is encountered. Any data following the Ctrl+Z is not read.
If you wish to read the entire file without this text-mode behaviour, use File.OpenRead
instead of File.OpenText
.
精彩评论