detect Ctrl+C with getc ?

P

PL

I read password from the console with _getcch to display no echo. The
problem is that I don't know how to flush extended char and ignore them,
like control+A, ...
In fact, I want to detect the Ctrl+C sequence in order to stop the input.
How can I do that ?
thanks
 
M

Martin Dickopp

PL said:
I read password from the console with _getcch to display no echo.

`_getcch' is not a standard C function.
The problem is that I don't know how to flush extended char and ignore
them, like control+A, ... In fact, I want to detect the Ctrl+C
sequence in order to stop the input. How can I do that ?

You cannot do that in standard C, which is the topic of this newsgroup.
Please ask in a group dedicated to your operating system.

Martin
 
N

nobody

PL said:
I read password from the console with _getcch to display no echo. The
problem is that I don't know how to flush extended char and ignore them,
like control+A, ...
In fact, I want to detect the Ctrl+C sequence in order to stop the input.
How can I do that ?
thanks

As Martin Dicko pointed out, _getcch is not Standard C. However, the
signal() function is in the standard. Your program is probably receiving
the signal represented by the macro SIGINT (in <signal.h>) when you
type Ctrl+C (the exact behavior of your console is platform-specific).

To ignore SIGINT:

#include <signal.h>
#include <stdio.h>

int main()
{
void (*old_handler)(int);

old_handler = signal(SIGINT, SIG_IGN);
if(old_handler == SIG_ERR)
{
perror("signal");
exit(1);
}
exit(0);
}

To flush out other control characters, you need to find out what
numerical values they correspond to on your system. A test program to
help you figure this out:

#include <stdio.h>

int main()
{
while(!feof(stdin) && !ferror(stdin))
printf("%d\n", getchar());

exit(0);
}

Simply type in control characters and make a note of the numbers that
come out. If your console is line-buffered, the program won't do anything
until you press Enter, then the numerical values for everything you typed,
including the Enter key, will print. Then the program waits for another
line of input.
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top