如何从文本文件中读取同一行两次?

时间:2016-05-09 11:05:53

标签: c#

我需要检查文件中的每一行。如果它等于"EOO"那么我将从循环中断开。如果不是,我需要处理该行。

问题是该行被跳过

while (r2.ReadLine() != "EOO")//check
{
    string temp = r2.ReadLine();
    if (temp == "Customer Name: " + name + "")
}

我做错了什么?

2 个答案:

答案 0 :(得分:4)

将值保存在variable中,并使用以下代码中该变量中保存的值:

var text = r2.ReadLine(); // read a line and save it in `text`

while (text != "EOO" && !r2.EndOfStream) // check for `EOO` or end of the file
{
    // your code, use the `text` variable instead of reading the next line
    if (text == $"Customer Name: {name}")
    {
        ...
    }

    text = r2.ReadLine(); // read the next line
}

答案 1 :(得分:0)

只需将它的值分配给while循环中的变量:

var line = null;

while((line = r2.ReadLine()) != "EOO")
{
    //Now process line, there you have the read value.
}