Read from Stdin without Writing to the Console
12
Using the Termios library, we can have the user enter a character on the keyboard, without it being displayed on the screen.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#include <memory.h>
int mygetch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
int main(int argc, char *argv[])
{
char answer;
while (answer == 'Y' || answer == 'y') {
printf("Continue? [Y/N] ");
answer = mygetch();
}
printf("Exiting...\n");
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#include <memory.h>
int mygetch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
int main(int argc, char *argv[])
{
char answer;
while (answer == 'Y' || answer == 'y') {
printf("Continue? [Y/N] ");
answer = mygetch();
}
printf("Exiting...\n");
return 0;
}
Comments
Voting
Votes Up
adorkable81
bertheymans
ctiggerf
dannyboy
jarfil
Pio
sehrgut
Sonsam
sundaramkumar
VuuRWerK
wiz1705






There are currently no comments for this snippet.