473,396 Members | 1,757 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,396 software developers and data experts.

Unbuffered character input

I am trying to understand the behavior of a short program. Hopefully
someone can tell me what's going on.

The program I wrote is:
#include <stdio.h>

main()
{
char c;
while(1)
{
c=getchar();
printf("The character pressed was %c\n",c);
if(c=='q')
break;
}
}
I run it, and start pressing keys, and nothing happens. When I hit
return, I see a series of lines on the screen, one for each character
I pressed. If one of those keys is 'q,' the program does not exit
until I press return. I guess this means the input is being
buffered. Seems simple enough.
Next, I tried running the same program, piping the output to less.
Now, I don't see any of the output until the program exit, at which
point less displays the entire output. The funny thing here is that
the input is not buffered: As soon as I press q, the program
terminates and all of the output is displayed by less.
Can someone explain to me why the input is buffered in the first case
but not in the second? Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.
Jun 27 '08 #1
4 5410

"Jules" <ju*********@gmail.comwrote in message
Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.
There is no standard method. As you have found out, the stdio functions are
line-based.
However most platforms provide a function called kbhit() or an equivalent.
Beware that you might have to call functions to set up the console and close
it again.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 27 '08 #2
On 18 May, 23:45, Jules <julianro...@gmail.comwrote:
I am trying to understand the behavior of a short program. Hopefully
someone can tell me what's going on.

The program I wrote is:
#include <stdio.h>

main()
{
char c;
while(1)
{
c=getchar();
printf("The character pressed was %c\n",c);
if(c=='q')
break;

}
}

I run it, and start pressing keys, and nothing happens. When I hit
return, I see a series of lines on the screen, one for each character
I pressed. If one of those keys is 'q,' the program does not exit
until I press return. I guess this means the input is being
buffered. Seems simple enough.
Next, I tried running the same program, piping the output to less.
Now, I don't see any of the output until the program exit, at which
point less displays the entire output. The funny thing here is that
the input is not buffered: As soon as I press q, the program
terminates and all of the output is displayed by less.
Can someone explain to me why the input is buffered in the first case
but not in the second?
I presume you're running your programme on a
Unix (like) operating system. The answer to your
question has nothing to do with standard C which
is what we discuss here.

out_of_topic {
I presume the input becomes unbuffered
because less arranges things these way.
You will notice that if you do
a.out | cat
the input is again buffered.
}
Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.
This is question 12.5 in the FAQ.
http://c-faq.com/

For Unix specific information you can ask
at comp.unix.programmer
Jun 27 '08 #3
On 19 May, 00:40, Spiros Bousbouras <spi...@gmail.comwrote:
On 18 May, 23:45, Jules <julianro...@gmail.comwrote:
I am trying to understand the behavior of a short program. Hopefully
someone can tell me what's going on.
The program I wrote is:
#include <stdio.h>
main()
{
char c;
while(1)
{
c=getchar();
printf("The character pressed was %c\n",c);
if(c=='q')
break;
}
}
I run it, and start pressing keys, and nothing happens. When I hit
return, I see a series of lines on the screen, one for each character
I pressed. If one of those keys is 'q,' the program does not exit
until I press return. I guess this means the input is being
buffered. Seems simple enough.
Next, I tried running the same program, piping the output to less.
Now, I don't see any of the output until the program exit, at which
point less displays the entire output. The funny thing here is that
the input is not buffered: As soon as I press q, the program
terminates and all of the output is displayed by less.
Can someone explain to me why the input is buffered in the first case
but not in the second?

I presume you're running your programme on a
Unix (like) operating system. The answer to your
question has nothing to do with standard C which
is what we discuss here.

out_of_topic {
I presume the input becomes unbuffered
because less arranges things these way.
^^^^
Eeeek.
You will notice that if you do
a.out | cat
the input is again buffered.

}
Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.

This is question 12.5 in the FAQ.http://c-faq.com/

For Unix specific information you can ask
at comp.unix.programmer
Jun 27 '08 #4
Malcolm McLean wrote, On 19/05/08 00:06:
>
"Jules" <ju*********@gmail.comwrote in message
>Also, if I want to get non-buffered input from
the keyboard, is there an easy way to do this? On my old computer, I
would use getch(), but I have learned that this is not standard, and
is not supported by the compiler I am using now.
There is no standard method. As you have found out, the stdio functions
are line-based.
However most platforms provide a function called kbhit() or an
equivalent. Beware that you might have to call functions to set up the
console and close it again.
If the OP is using a Unix like system (and use of less suggests but does
not guarantee this) then it almost certainly has a different name and
operates differently. comp.unix.programmer would probably be a
reasonable place for the OP to ask after checking their FAQ.
--
Flash Gordon
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Max Rabkin | last post by:
While java's System.in is, according to the javadocs, an InputStream, it is in fact an instance of java.io.BufferedInputStream. Is there any way to have unbuffered access to it without hacking the...
1
by: Starbase Commander | last post by:
My Perl books don't explain how to receive single keystrokes in real time, like the basic statement "inkey$" or C statement "getc" - probably because I assume it requires a specific system call...
5
by: Rich | last post by:
Does anyone know the correct way of opening an unbuffered output file using STL? I attempted: ofstream myfile("fname.txt", ios::binary); myfile.setbuf(NULL, 0); and I was informed that...
1
by: | last post by:
Is this even possible? I've found some references to specific "unbuffered" type methods that exist in older incarnations of basic_streambuf but not in newer ones. Info please. :P
11
by: Lonnie Princehouse | last post by:
>From the cmd shell on both Windows 2k and XP, I'm getting this weird syntax error in conjunction with the unbuffered flag. It works fine without -u. Has anyone else encountered it? This didn't...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
19
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
7
by: Markus Mayer | last post by:
Hi folks. I'm somewhat new to *nix programming and just ran into a problem. I have to take user input from the terminal but like to constrain that to some rules given, i.e. "numbers only" or...
4
by: yomama | last post by:
I am looking to take unbuffered, unechoed input from the user. The idea is to print "Press any key to continue" and allow the user to do just that, without having to press enter, and without having...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.