main() {
char buffer[1000];
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
printf("Line read: %s", buffer);
}
printf("\nPress Enter to exit...\n");
getchar();
return 0;
}
I want my app to read stdin, then after i finished entering data, program should print "Press enter" and wait for user input before finishing. But now it is finishing immediately
I tried to launch this way:
./a.out < test
or just
./a.out
And then after finishing input data, i press ctrl+D and its still just immediately exits without waiting for confirmation. What am i doing wrong ?
Try printf("\nPress Enter to exit...\n"); while (getchar() != '\n');
And i just got infinite loop i think
the input buffer is not empty after returning from the while, so getchar() returns immediately
Oh, okay. How i can clear it after fgets then ?
you can read from it until it blocks
Стикер
Стикер
Hm. Interesting. Thanks for info
I believe this is what you need: https://www.gnu.org/software/libc/manual/html_node/Noncanon-Example.html This is an example to handle CTRL + D so that the read() syscall is aware of that. The CTRL + D will yield an EOT char '\004'. Nore that this only works when the stdin is a terminal. Doing ./bla < file doesn't work in this approach.
Обсуждают сегодня