getch?

P

Profetas

Hi,

How do I read a char from stdin before the '\n' is pressed?

for example I know that I can use getch() to read a single char from the
user input, but suppose I wanted to read before the carriage return is
pressed.

for example I type a char and the following actions is done before the
'\n' is pressed.

Thanks
 
M

Michael Mair

Profetas said:
Hi,

How do I read a char from stdin before the '\n' is pressed?

for example I know that I can use getch() to read a single char from the
user input, but suppose I wanted to read before the carriage return is
pressed.

for example I type a char and the following actions is done before the
'\n' is pressed.

FAQ 19.4

Your questions *always* are FAQ slightly rewritten -- either you
are doing this to keep us busy or you are too lazy to read the thing.
Not fair. As promissed:

*PLONK*
 
E

Eric Sosman

Profetas said:
Hi,

How do I read a char from stdin before the '\n' is pressed?

This is Question 19.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
for example I know that I can use getch() to read a single char from the
user input, but suppose I wanted to read before the carriage return is
pressed.

<OT>Has anyone else noticed that on this newsgroup
the phrase "I know" almost always introduces a false
statement, or at the very least a gross misunderstanding?
I know I can't be the only one who-- oh, drat!</OT>

There is no getch() function in Standard C. If your
C implementation happens to provide one, it's some kind
of extension to C, tacked on rather than inherent.
for example I type a char and the following actions is done before the
'\n' is pressed.

FAQ.
 
C

Christopher Benson-Manica

Eric Sosman said:
<OT>Has anyone else noticed that on this newsgroup
the phrase "I know" almost always introduces a false
statement, or at the very least a gross misunderstanding?

Which is why I typically forego using it; it lessens the sting when I
turn out to be wrong ;)
 
P

Profetas

NAME
getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
push back)
characters from curses terminal keyboard

SYNOPSIS
#include <curses.h>

int getch(void);
int wgetch(WINDOW *win);
int mvgetch(int y, int x);
int mvwgetch(WINDOW *win, int y, int x);
int ungetch(int ch);
int has_key(int ch);

DESCRIPTION
The getch, wgetch, mvgetch and mvwgetch, routines read a
character from
the window. In no-delay mode, if no input is waiting, the value
ERR is
returned. In delay mode, the program waits until the system
passes text
through to the program. Depending on the setting of cbreak, this
is after
one character (cbreak mode), or after the first newline (nocbreak
mode).
In half-delay mode, the program waits until a character is typed
or the
specified timeout has been reached.
 
J

Joona I Palaste

Profetas said:
NAME
getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
push back)
characters from curses terminal keyboard
SYNOPSIS
#include <curses.h>

Curses is a platform-specific extension to C and not a part of ISO
standard C.
 
D

Dan Pop

In said:
Curses is a platform-specific extension to C and not a part of ISO
standard C.

It is not part of ISO standard C, but it is the best you can get in terms
of platform *independent* API for full screen programming. You're
likely to find a curses implementation for every hosted implementation
on which full screen programming makes sense.

OTOH, it is ludicrous to use curses if all you want is the ability to
get direct access to the user input.

Dan
 
J

John Bode

Profetas said:
NAME
getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or
push back)
characters from curses terminal keyboard

SYNOPSIS
#include <curses.h>

int getch(void);
int wgetch(WINDOW *win);
int mvgetch(int y, int x);
int mvwgetch(WINDOW *win, int y, int x);
int ungetch(int ch);
int has_key(int ch);

DESCRIPTION
The getch, wgetch, mvgetch and mvwgetch, routines read a
character from
the window. In no-delay mode, if no input is waiting, the value
ERR is
returned. In delay mode, the program waits until the system
passes text
through to the program. Depending on the setting of cbreak, this
is after
one character (cbreak mode), or after the first newline (nocbreak
mode).
In half-delay mode, the program waits until a character is typed
or the
specified timeout has been reached.


Lovely. However, curses is not a part of the standard C library, and
is not a part of every C implementation out there (I don't think
you're going to find curses in MSVC). curses is an extension,
provided by a specific implementation.

C does not provide high-level (built-in) support for reading
individual keystrokes; this is something that's specific to your
particular platform, and this question would be more topical in a
group devoted to your platform. The answer for *nix is different from
the answer for Windows, which is different from the answer for VMS,
etc.
 
M

Merrill & Michele

John Bode said:
"Profetas" <[email protected]> wrote in message


Lovely. However, curses is not a part of the standard C library, and
is not a part of every C implementation out there (I don't think
you're going to find curses in MSVC). curses is an extension,
provided by a specific implementation.

C does not provide high-level (built-in) support for reading
individual keystrokes; this is something that's specific to your
particular platform, and this question would be more topical in a
group devoted to your platform. The answer for *nix is different from
the answer for Windows, which is different from the answer for VMS,
etc.

Mr. OP--

Might you be so kind as to provide the text to the non-Standard header file
in question? Around here, it seems to be the punchline to some joke I don't
know. MPJ

P.S. Honestly it sounds like Harry Potter. Isn't Migwetch one of the
fraternities in which W was smart?
 
J

Joona I Palaste

Merrill & Michele said:
Might you be so kind as to provide the text to the non-Standard header file
in question? Around here, it seems to be the punchline to some joke I don't
know. MPJ

Merely having the header file won't help you. Header files don't contain
code for functions - merely definitions. The actual code is stored in
libraries, which aren't necessarily portable across implementations.
This is all explained in the comp.lang.c FAQ.
 
M

Mike Wahler

Joona I Palaste said:
Merely having the header file won't help you. Header files don't contain
code for functions - merely definitions.

Declarations. But you knew that. :)

-Mike
 
J

Joona I Palaste

Declarations. But you knew that. :)

Yes. Dammit Jim, I mean Mike, those words are too similar. In Finnish
"definition" is "määritelmä" and "declaration" is "julistus". It would
be so much better if we all spoke Finnish here. =)
 
M

Merrill & Michele

Joona: Yes. Dammit Jim, I mean Mike, those words are too similar. In Finnish
"definition" is "määritelmä" and "declaration" is "julistus". It would
be so much better if we all spoke Finnish here. =)

Your use of dammit jim does not follow the standard prototype. It always
goes, "Dammit Jim, I'm a doctor not a ...." And if we switch languages in
clc, it's not going to be one where Pop has the drop on us. MPJ
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top