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

Portable way to wait for a keypress

Hi,

I've been working on this C assignement for a CS course.... the
assignement is going pretty well and all my code works well on both
Windows and Linux.

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).

Here's what I have :

void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}

The fflush() is there to get rid of some leftover \n in the input queue
(from a previous scanf() for instance). It works under Windows (the
fflush(stdin) works), but it doesn't under Linux. And I already know
that "fflush(stdin)" is wrong (fflush() is supposed to be for output
buffers, not input... or something like that).

So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?

Thanks for your help !

Alex....
Nov 14 '05 #1
7 14811
Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D
Nov 14 '05 #2
Alex007 <adery_re_move_@really_hotmail.com> writes:
[...]
void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.

fflush(stdin) invokes undefined behavior; see question 12.26.

There is no portable way to read a single character from the keyboard
without waiting for the end of a line; see question 19.1. (Input may
be line-buffered; there's no portable way to turn this buffering off.)

But since your prompt is "Press Enter to continue...", you probably
want to read and discard everything up to the end of a line (i.e.,
until the user presses Enter).

Output may also be line-buffered, but you can override this with
fflush(stdout), which you should do immediately after the printf call.
Follow this with a loop that reads characters from stdin, terminating
when it sees a newline ('\n') character.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
On Mon, 14 Jun 2004, Alex007 wrote:
Hi,

I've been working on this C assignement for a CS course.... the
assignement is going pretty well and all my code works well on both
Windows and Linux.

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).

Here's what I have :

void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}

The fflush() is there to get rid of some leftover \n in the input queue
(from a previous scanf() for instance). It works under Windows (the
fflush(stdin) works), but it doesn't under Linux. And I already know
that "fflush(stdin)" is wrong (fflush() is supposed to be for output
buffers, not input... or something like that).

So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?
The only portable way for this to work is to make sure that all other
input gathering does not leave behind unwanted data on the input stream.
Otherwise you cannot have a portable way of handling this.
Thanks for your help !

Alex....


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4
Alex007 wrote:

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).


See the FAQ:

http://www.eskimo.com/~scs/C-faq/q19.1.html

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 14 '05 #5
Darrell Grainger wrote:
On Mon, 14 Jun 2004, Alex007 wrote:

.... snip ...

So what do you gurus think ? What would be a right/portable way
to wait for a keypress (any key if possible, not just ENTER) and
be sure that no "leftover" \n will jinks it ?


The only portable way for this to work is to make sure that all
other input gathering does not leave behind unwanted data on the
input stream. Otherwise you cannot have a portable way of
handling this.


Or alternatively to ensure all other input routines leave at least
a '\n' in the input stream. Then you can use:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #6
Alex007 <adery_re_move_@really_hotmail.com> writes:
Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to
wait for a keypress (any key if possible, not just ENTER) and be
sure that no "leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D


There's probably a system-specific way to do this under Windows, and
another system-specific way to do it under Linux. Check the
documentation for each system (or, failing that, ask in a
system-specific newsgroup for each system). Then use #if or #ifdef to
select the appropriate code for each system.

Something like this:

#if defined(__linux__)
/* Linux-specific code to wait for a keypress */
#elif defined(__windows__)
/* Windows-specific code to wait for a keypress */
#else
#error "Unsupported system"
#endif

I don't know whether the symbol __windows__ is defined on Windows
systems.

You'll probably also need to use #ifdef to select system-specific
headers.

Or you might consider waiting for the enter key, rather than for any
keypress; you can do that portably.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Alex007 wrote:

Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D


Nothing says you are forbidden from ever using an implementation-specific
feature, does it?

Isolate this "wait for a keypress" routine as a single function, called as
needed, and simply write a different version for each platform you need to
support.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #8

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

Similar topics

22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
1
by: TheOneRedDragon | last post by:
At the moment, I have a command-line application which prints out quite a lot of text to the screen. However, when Windows users run it, the prompt disappears before they can read any of it. Is...
4
by: Brendan Miller | last post by:
I want to spawn a child process based on an external executable that I have the path for. I then want to wait on that executable, and capture it's output. In the os module, fork is only...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...

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.