By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!!!
Mar 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Henry <He***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #2

P: n/a
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!
"Jon Skeet [C# MVP]" wrote:
Henry <He***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '06 #3

P: n/a
Henry <He***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #4

P: n/a
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!

"Jon Skeet [C# MVP]" wrote:
Henry <He***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.