简体   繁体   中英

Using Console.Read() to determine end of file C#

A few days ago I've asked a question concerning how to detect an end of input file of N(N is unknown) lines.

StringBuilder input = new StringBuilder();

int endOfFile = 0
while ((endOfFile = Console.Read()) != -1) {
    input.Append(((char)endOfFile).ToString());
    input.Append(Console.ReadLine());
}

I've edited my question, but I guess this is the same as some of the hints below.

@Jagannath basically had it, exactly as you asked, except for one little detail: That method works on the console as well, without involving an explicit StreamReader:

string line;
while ((line = Console.ReadLine()) != null)
{
    // TODO: Add processing
    Console.WriteLine(line);
}

If you're typing directly in the console instead of relying on input redirected from a file, press CTRL-Z or F6 to trigger an "end of file" on the console input. Inside the console, F6 is just a synonym of CTRL-Z .

Be aware that CTRL-Z here is an interrupt sequence or signal, not an input character. It will show ^Z on the screen. but you will not receive the CTRL-Z (U+001A) character in the program. The CTRL-Z sequence is trapped by the console and causes the input stream to close as if the "file had reached the end". Do not insert a CTRL-Z in the input file.

The origin of using CTRL-Z for this purpose goes back to at least the CP/M operating system (a short entertaining story on its own right, but one which is out of scope).

This could help in looping through the file and check for end of file.

using (StreamReader sr = new StreamReader(@"test.txt"))
{
       string line;
       while ( (line = sr.ReadLine()) != null) 
       {
           Console.WriteLine(line);
       }
}

UPDATE

Here is a link from msdn ReadLine method

Are you planning on using the shell to redirect standard input to your input file?

Why not use something like TextReader.Read() or TextReader.ReadLine()? http://msdn.microsoft.com/en-us/library/system.io.textreader.aspx

Not quite sure why you're asking this, since the answer you accepted to your previous question is the easiest way to do what you were asking there. Are you just looking for alternate solutions?

You should check the documentation for Console.ReadKey . In the Remarks it says:

The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.

ReadKey will block until you press a key on the keyboard. You can't use ReadKey to detect the end of redirected input.

To read input from a file you can use

var text = File.ReadAllLines("path/to/file.txt");

If what you want is to get input from the console character by character, you can do something like this:

ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey()).Key != ConsoleKey.Escape)
{
}

You can replace ConsoleKey.Escape with any other key, or check for key combinations like CTRL+D, CTRL+C, by using keyInfo.Modifiers .

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