简体   繁体   中英

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. 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 " ". 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.

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:

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. Make the while loop like this...

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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