StreamReader的的ReadLine返回null时不EOF [英] StreamReader ReadLine returns null when not EOF

查看:1082
本文介绍了StreamReader的的ReadLine返回null时不EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说完,我从来没有遇到过,也没有听说发生了一个奇怪的问题。看来,偶尔,StreamReader类的ReadLine()函数将返回NULL,因为如果它是在文件的结尾,但它不是。

Having a strange problem that I've never encountered nor heard of happening. It seems that occasionally, the ReadLine() function of the StreamReader class will return NULL, as if it's at the end of the file, BUT it's not.

我的日志文件说明一切刚刚发生的事情,就好像它实际上已经达到了文件的末尾,却不失它只是处理的一部分。虽然目前没有出现任何一致性,因为如果我重新从头开始过程中,整个文件没有发生任何事件处理。

My log file indicates that everything is happening just as if it had actually reached the end of the file, but yet it's only processing part of it. There doesn't appear to be any consistency, because if I restart the process from scratch, the whole file is processed without incident.

显然,没有什么时髦文件本身,否则会每次都这样做在同一行,再加上它已经发生了几个不同的文件,他们每次都重新运行,它工作正常。

Clearly, there is nothing funky in the file itself, or it would do this on the same line each time, plus it has happened with a few different files, and each time they are re-run, it works fine.

任何人都跨越任何类似运行,或者有什么可能会导致这样的事情有什么建议?

Anyone run across anything similar, or have any suggestions on what might be causing such a thing?

谢谢,

安德鲁

示例:

line = _readerStream.ReadLine();

if (null != line)
{
    eventRetVal = loadFileLineEvent(line);
}
else
{
    // do some housecleaning and log file completed
}

_readerStream是已经在别处打开流

_readerStream is the stream which has been opened elsewhere.

loadFileLineEvent是被传递在处理该线的委托。这有它自己的错误处理(有记录),所以没有问题在那里。

loadFileLineEvent is a delegate that gets passed in which processes the line. This has its own error handling (with logging), so there's no issue in there.

以上(未全部显示)的程序不具有的错误围绕它的处理也(日志记录),这是不被任何触发。

The routine above (not shown in its entirety) has error handling around it also (with logging), which is not being triggered either.

这是越来越以别人的记录,它达到了文件的末尾,但它是从数量明显记录我拿到它没有。

It's getting to the "else" and logging that it reached the end of the file, but it's obvious from the number of records I got that it didn't.

推荐答案

你试过一个更传统的方法来读取该流?读取下一个可能的空/空行之前的流的末尾,这样你的检查。好像你的代码应该工作,但抛出试图读取并不存在行(如果SR抛出的,虽然不知道)一个可能的空例外。

Have you tried a more traditional approach to reading the stream? This way your checking for the end of the stream before reading the next potentially empty/null line. Seems like your code should work, but with a possible null exception thrown for trying to read a line that doesn't exists(not sure if SR throws for that though).

 using (StreamReader SR = new StreamReader(OFD.FileName))
 {
     while (!SR.EndOfStream)
     {
         string CurrentLine = SR.ReadLine();
         var eventRetVal = loadFileLineEvent(CurrentLine);
     }
 }

这篇关于StreamReader的的ReadLine返回null时不EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆