I'm trying to read a line from stdin but I don't know to properly handle the cases when input size is at least equal to the limit. Example code:
void myfun() { char buf[5]; char somethingElse; printf("\nInsert string (max 4 characters): "); fgets (buf, 5, stdin); ... printf("\nInsert char: "); somethingElse = getchar(); } Now, the user can do three things:
- Input less than 4 characters (plus newline): in this case there's nothing left in stdin and the subsequent
getchar()correctly waits for user input; - Input exactly 4 characters (plus newline): in this case there's a newline left in stdin and the subsequent
getchar()reads it; - Input more than 4 characters (plus newline): in this case there's at least another character left in stdin and the subsequent
getchar()reads it, leaving at least a newline in.
Cases 2 and 3 would require emptying stdin using something like while(getchar() != '\n'), whereas case 1 doesn't require any additional action. As I understand from reading answers to similar questions and c-faq, there's no standard/portable way to know whether the actual scenario is the one described in 1 or not.
Did I get it well? Or there actually is a portable way to do it? Or maybe a totally different approach?
buf[]contains a newline?\ngoes also into the buffer.