C# 文件未被完全读取
我有一个大约113687行的文件文本,但我的应用程序只读取314行,有人能说为什么吗 我的代码:C# 文件未被完全读取,c#,.net,file,C#,.net,File,我有一个大约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); // ... } 带有功能的
string file = @"z:\foo.txt";
StreamReader reader = File.OpenText(file);
string line;
int rows = 0;
while ((line = reader.ReadLine()) != null) {
++rows;
doSomethingWith(line);
// ...
}
带有功能的dosomething类似于:
protected static bool DoSomethingWith(string line)
{
return Regex.Match(line, @"\d+\-\d+\.\d+\.\d+\.\d+").Success;
}
更新:
在回答格雷格的问题时:
您的foo.txt是否在第314行包含Ctrl+Z字符
是的,我的文件在第314行包含一个Control-Z
字符。Windows上的文本文件可以用Ctrl+Z字符终止。这意味着在读取文件时,遇到Ctrl+Z时,StreamReader
返回文件结尾。Ctrl+Z之后的任何数据都不会被读取
如果您希望在没有这种文本模式行为的情况下读取整个文件,请使用file.OpenRead
而不是file.OpenText
因为您没有提供足够的信息,所以没有人能说出原因。我们如何知道doSomethingWith(line)
不计算到314然后崩溃?您的foo.txt
是否在第314行包含Ctrl+Z字符?