I am a software student looking for a way to get rid of characters that might be in stdin. I tried the method that was given here in various forms, where you do something like this:
void clearStdIn(void) { char c; while((c = getchar()) != '\n' && c != EOF) /* discard */ ; }
The problem is that if there is nothing in stdin to start with, this function sits nearby, waiting for the user to press the enter button before the control flow can move on. What should I do?
Flushing the input stream (portable way) without blocking can be performed as follows:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> int flush_inputstream(int fd) { int result = 0; int flags = fcntl(fd, F_GETFL); if (-1 == flags) { perror("fcntl() failed getting flags"); result = -1; goto lblExit; } if (!(flags & O_NONBLOCK)) /* If stream isn't non-blocking */ { /* set it to be non-blocking. */ result = fcntl(fd, F_SETFL, O_NONBLOCK); if (-1 == result) { perror("fcntl() failed setting O_NONBLOCK"); goto lblExit; } } /* Loop reading from the stream until it is emtpy: */ do { char c = 0; ssize_t bytesRead = read(fd, &c, 1); if (-1 == bytesRead) { if ((EAGAIN != errno) && (EWOULDBLOCK != errno)) { perror("read() failed"); result = -1; } break; } } while (1); if (!(flags & O_NONBLOCK)) /* If stream had not be non-blocking */ { /* re-set it to not be non-blocking. */ int result_fcntl = fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); if (-1 == result_fcntl) { perror("fcntl() failed setting flags"); if (0 == result) /* Do not overwrite prvious error! */ { result = result_fcntl; } goto lblExit; } } lblExit: return result; } /* To test this: */ int main(void) { int fd = fileno(stdin); printf("Feed some chars via the keyboard now!\n"); sleep(3); printf("Game Over! Press enter to see stdin is empty\n"); if (-1 == flush_inputstream(fd)) { fprintf(stderr, "flush_inputstream() failed"); return EXIT_FAILURE; } char s[16] = ""; if (NULL == fgets(s, sizeof(s), stdin)) { perror("fgets() failed"); } printf("%s\n", s); return EXIT_SUCCESS; }
. -, . , "" (, ) , "" (, ), , .
, , , , , , script . STDIN ( STDOUT , , ). STDIN, .
, . ( - ), .. (, ), , , . - , "fetch whole line of user input" (, gets()) , .
gets()
, / , - " ...", / . , , - (, ) . " : 22 " - , , "22", "", ( " - ", " " ). , 22 .
, "" , , , , .
If you have this, you can use select()to detect that there is an input, and then do a reading to cancel it. I would recommend making a "big" block read in this case, and not just one character at a time.
select()
Note that getchar()returns aint constant that EOFdoes not fit in a character. This is an “out of communication zone” example ; it is not a symbol.
getchar()
int
EOF
Source: https://habr.com/ru/post/1526911/More articles:Snap.svg - Перезапуск события остановки навешивается на подэлемент надвигающегося элемента - javascriptWriting a function to get all subsequences of size n in Haskell? - functional-programmingHow to programmatically add a self-signed certificate to create an HTTPS request from Java code? - javaHow to clear input buffer? (C) - cReplacing characters in String - javaUsing stdin with select () in C - cPsutil повышает AccessDenied для всех процессов, не принадлежащих пользователю, при получении атрибутов процесса - pythonSQL select top counts for grouped rows - sqljavascriptWPF уведомляет PropertyChanged для свойства Get - c#All Articles