2011-10-04 90 views
-3

我有一個大約113687行的文件文本,但我的應用程序只讀取314行,任何人都可以說爲什麼?文件未完全讀取

我的代碼:

string file = @"z:\foo.txt"; 
StreamReader reader = File.OpenText(file); 
string line; 
int rows = 0; 
while ((line = reader.ReadLine()) != null) { 
    ++rows; 
    doSomethingWith(line); 
    // ... 
} 

DoSomethingWith功能類似於:

protected static bool DoSomethingWith(string line) 
{ 
    return Regex.Match(line, @"\d+\-\d+\.\d+\.\d+\.\d+").Success; 
} 

更新時間:

在回答Gregs問題:

您的foo.txt在第314行包含Ctrl + Z字符嗎?

是的,我的文件包含上線Control-Z字符在Windows 314

+4

沒有人可以說爲什麼,因爲你沒有提供足夠的信息。我們怎麼知道'doSomethingWith(line)'不算314,然後崩潰? –

+2

你的'foo.txt'在第314行包含一個Ctrl + Z字符嗎? –

+0

@Greg Hewgill:是的。 –

回答

4

文本文件可以用按Ctrl +ž字符被終止。這意味着當文件被讀取時,StreamReader返回Ctrl + Z遇到文件結束。以下任何數據:Ctrl + Z未被讀取。

如果您希望讀取沒有此文本模式行爲的整個文件,請使用File.OpenRead而不是File.OpenText