Strange thing about System.Console.Read()???

G

Guest

for the following code snippet:

int k;
char c;
while (true)
{
k = Console.Read ();
if (k == -1) break;
c = (char) k;
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");

jljkj
Echo: j
Echo: l
Echo: j
Echo: k
Echo: j
Echo:
Echo:

so this is result after I typing "jljkj" then "enter" in the console in XP.
What is going on here!? I just don't understand the program's performance?
Read() is suppose to read in a character? or a stream!? But even it can read
in a stream, how can it separates the character in the stream and print them
out separately according to code above!? I am new to C#. Thanks for your
help!!!
 
J

Jon Skeet [C# MVP]

Henry said:
for the following code snippet:

int k;
char c;
while (true)
{
k = Console.Read ();
if (k == -1) break;
c = (char) k;
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");

jljkj
Echo: j
Echo: l
Echo: j
Echo: k
Echo: j
Echo:
Echo:

so this is result after I typing "jljkj" then "enter" in the console in XP.
What is going on here!? I just don't understand the program's performance?
Read() is suppose to read in a character? or a stream!? But even it can read
in a stream, how can it separates the character in the stream and print them
out separately according to code above!? I am new to C#. Thanks for your
help!!!

It reads a character at a time, but it only gets to see anything after
you've hit enter. If you're using C# 2.0, you can use Console.ReadKey
instead.
 
G

Guest

Thank you very much Jon, you answer makes sense, but why at the end, it
echoes twice of the "enter"? I just don't get it. Thanks a lot!

And in MSDN reference, it says:
Return Value
The next character from the input stream, or negative one (-1) if no more
characters are available.

How do I enter a "-1" in the console? Thanks a lot!
 
J

Jon Skeet [C# MVP]

Henry said:
Thank you very much Jon, you answer makes sense, but why at the end, it
echoes twice of the "enter"? I just don't get it. Thanks a lot!

That's the carriage return and the line feed. Print out the key as a
number instead of a character (just don't do the cast - leave it as an
int) and you'll see what I mean.
And in MSDN reference, it says:
Return Value
The next character from the input stream, or negative one (-1) if no more
characters are available.

How do I enter a "-1" in the console? Thanks a lot!

You don't, from an interactive console. If you run something like:

test < foo.txt

you'll find that you get to the end of the input stream at the end of
the file.
 
G

Guest

Thank you so much john, you are really an expert:) I have tried, and the
first one is (13) which is a carriage returen, then second one is (10) which
is he NewLine feed

BTW: in fact you can enter a (-1) from the console, which is Ctrl + z

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top