I would have expected the following C# program to only print \"EOF!\" once I hit \"Ctrl-Z\" in the console. Instead, the program finishes as soon as I hit Enter:
Hitting Ctrl-Z will produce the value 26 from Console.In.Peek();
You have to close the input stream in order to produce a -1 (happens when you close the console, hit Ctrl-C (by default), or explicitly call Console.In.Close()
).
Also, by default the console streams will operate in line mode, which means that the stream won't actually be filled with characters until you hit enter. You can use 'Console.ReadKey', which blocks (see http://msdn.microsoft.com/en-us/library/system.consolekeyinfo.key.aspx), or you can switch the console out of line mode. A C# example of this can be found here: http://ewbi.blogs.com/develops/2005/11/net_console_pre.html.
Console.In.Read() returns -1 on EOF, So you can do this:
int c;
while((c = Console.In.Read()) != -1)
Console.Out.Write((char)c);