简体   繁体   English

测试C#文本文件中是否没有更多文本

[英]Test if there's no more text in text file in c#

My code looks like this: 我的代码如下所示:

 System.IO.StreamReader savednames =
                new System.IO.StreamReader(@"c:\users\example\text.txt");
            while ((line = savednames.ReadLine()) != null) 
            {
                if (line == null)
                {
                    Console.WriteLine("No more content!");
                    }         }

(yes there is code when the while is true but it's not relevant here) This doesn't work, and i've searched online a whole lot but haven't found an answer. (是的,当while为true时有代码,但是在这里不相关),这是行不通的,我在网上搜索了很多,但没有找到答案。 I want to check if the next line in the text document is empty, and if it is, say "no more lines" or something simular.The line isn't null, nor " ". 我想检查文本文档中的下一行是否为空,如果是,请说“ no more lines”或类似符号。该行既不为null,也不为“”。 Would appreciate some help! 希望能有所帮助!

This is happening because when you reach the end of the file, the condition for the while loop is false and the body is not entered, so the message is not printed. 发生这种情况的原因是,当到达文件末尾时,while循环的条件为false,并且未输入正文,因此不会打印该消息。

Change the loop as follows: 如下更改循环:

while (true)
{
    var line = savednames.ReadLine();

    if (line == null)
    {
        Console.WriteLine("No more content!");
        break;
    }
}

Note that this will only terminate the loop when the end of file has been reached. 请注意,这仅在到达文件末尾时才终止循环。 It will NOT terminate it simply because one of the lines in the file is empty. 它不会仅仅因为文件中的一行为空而终止它。

If you want to terminate the loop on an empty line, use: 如果要在空行处终止循环,请使用:

while (true)
{
    var line = savednames.ReadLine();

    if (string.IsNullOrEmpty(line))
    {
        Console.WriteLine("No more content!");
        break;
    }
}

And if you want to terminate the loop on an empty or blank line use: 如果您想在空行或空白行上终止循环,请使用:

while (true)
{
    var line = savednames.ReadLine();

    if (string.IsNullOrWhiteSpace(line))
    {
        Console.WriteLine("No more content!");
        break;
    }
}

This kind of loop structure is quite commonly used when reading files. 这种循环结构在读取文件时非常普遍。 It's known as a "loop and a half". 它被称为“半圈”。


Please note that there are probably better ways to do this, which avoid using a "loop and a half" (which some people don't like). 请注意,可能有更好的方法来执行此操作,避免使用“半圈”(有些人不喜欢)。

You can, for example, use the File.ReadLines() method to get all the lines from a file, without buffering: 例如,您可以使用File.ReadLines()方法从文件中获取所有行,而无需进行缓冲:

foreach (string line in File.ReadLines(@"c:\users\example\text.txt"))
{
    // Do something with line                
}

Console.WriteLine("No more content!");

If you want to only process up until the first non-empty line (like in the second loop example above) you would write it like so: 如果只想处理到第一条非空行(如上面的第二个循环示例),则可以这样编写:

var linesUntilFirstEmpty = 
    File.ReadLines(@"c:\users\example\text.txt")
    .TakeWhile(line => !string.IsNullOrEmpty(line));

foreach (string line in linesUntilFirstEmpty)
{
    // Do something with line                
}

Console.WriteLine("No more content!");

And if you wanted to process lines until the first blank or empty one: 如果要处理行直到第一个空白或为空,请执行以下操作:

var linesUntilFirstBlankOrEmpty = 
    File.ReadLines(@"c:\users\example\text.txt")
    .TakeWhile(line => !string.IsNullOrWhiteSpace(line));

foreach (string line in linesUntilFirstBlankOrEmpty)
{
    // Do something with line                
}

Console.WriteLine("No more content!");

To achieve your expected output, you need to change condition of while loop. 为了获得预期的输出,您需要更改while循环的条件。 Make the while loop like this... 像这样使while循环...

while (true)       
{    
    var line = savednames.ReadLine();

    if (string.IsNullOrEmpty(line))
    {
        Console.WriteLine("No more content!");
        break;
    }
} 

I hope this is what you were looking for... 我希望这是您想要的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM