streamreader
中是否有一个函数允许查看/阅读下一行以获取更多信息,而无需实际将迭代器移动到下一个位置?
当前行的 Action 取决于下一行,但我想保持使用这个代码块的完整性
while((Line = sr.ReadLine())!=null)
最佳答案
原则上,没有必要做这样的事情。如果你想关联两个连续的行,只需根据这个事实调整分析(在阅读第 2 行的同时执行第 1 行的操作)。示例代码:
using (System.IO.StreamReader sr = new System.IO.StreamReader("path"))
{
string line = null;
string prevLine = null;
while ((line = sr.ReadLine()) != null)
{
if (prevLine != null)
{
//perform all the actions you wish with the previous line now
}
prevLine = line;
}
}
这可能会根据需要进行调整以处理尽可能多的行(以前行的集合而不仅仅是 prevLine
)。
关于c# - readline 然后将指针移回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360110/