我需要检查文件中的每一行。如果它等于"EOO"
那么我会从循环中断开。如果没有,我需要处理该行。如何从文本文件中读取同一行两次?
的问题是该行被跳过
while (r2.ReadLine() != "EOO")//check
{
string temp = r2.ReadLine();
if (temp == "Customer Name: " + name + "")
}
我在做什么错?
我需要检查文件中的每一行。如果它等于"EOO"
那么我会从循环中断开。如果没有,我需要处理该行。如何从文本文件中读取同一行两次?
的问题是该行被跳过
while (r2.ReadLine() != "EOO")//check
{
string temp = r2.ReadLine();
if (temp == "Customer Name: " + name + "")
}
我在做什么错?
保存在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
}
如果没有'EOO'的文件,也许你应该包含'null'来避免无限循环。我也错过了''客户名称:“+名称'检查。 –
您在回答中使用的'EndOfStream'比我检查'null'要好。包括其他支票,认为这很明显,但我想这不是OP。谢谢@Tim –
就分配给它的价值,是一个变量在while循环:
var line = null;
while((line = r2.ReadLine()) != "EOO")
{
//Now process line, there you have the read value.
}
或许有是以某种方式在变量中存储'r2.ReadLine()'的一种方式。 –
您可能需要将字符串加载到内存中,然后进行检查。 –
你的问题是因为你调用.ReadLine()两次......调用它一次,本地存储它然后读取变量。 –