How to read a line from stdin, blocking until the newline is found?
问题 I'm trying to read one line at a time, of arbitrary length, from stdin at the command line. I'm not sure if I'll be able to include GNU readline and would prefer to use a library function. The documentation I've read suggests that getline ought to work, but in my experiments it doesn't block. My sample program: #include <stdio.h> int main() { char *line = NULL; if (getline(&line, NULL, stdin) == -1) { printf("No line\n"); } else { printf("%s\n", line); } return 0; } produces No line , which