473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
4 4492
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
5
by: Terry | last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a "recvFrom()" call and other thread sends a UDP packet to some address, that if the machine on the other end isn't up,...
10
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the...
10
by: Curious | last post by:
Hi, I have a worker thread running, where basically its operation is to execute in an infinite loop. Now, when I am suspending this thread, the CPU usage time is still remaining 100%. What...
0
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named...
5
nikpreek
by: nikpreek | last post by:
Hi All, This is my first post, I've been finding lots of solutions from this forum. Thanks to all who helped (and ones who asked questions). I have a strange problem. I'm having a console app and...
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
2
by: Jan Obrestad | last post by:
Hello, I've been using the XmlReader class to read xml files. Lately it has had some strange quirks. It seems to ignore elements when there is no whitespace between them. ex...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.