How to read input of unknown length using fgets
问题 How am I supposed to read long input using fgets() , I don't quite get it. I wrote this #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char buffer[10]; char *input; while (fgets(buffer,10,stdin)){ input = malloc(strlen(buffer)*sizeof(char)); strcpy(input,buffer); } printf("%s [%d]",input, (int)strlen(input)); free(input); return 0; } 回答1: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char buffer[10]; char *input = 0; size_t cur_len = 0;