I am having a little problem with fgets when the input string exceeds a predefined limit.
Taking the example below:
for(index = 0; index < max; index++) {printf(" Enter the %d string : ",index+1) if(fgets(input,MAXLEN,stdin)) { printf(" The string and size of the string is %s and %d \n",input,strlen(input) + 1); removeNewLine(input); if(strcmp(input,"end") != 0) { //Do something with input } }
Now that I have exceeded the length of MAXLEN and enter the string, I know that the input will add "\ 0" to MAXLEN -1, and that will be so. The problem occurs when I try to enter a second line that is not requested for ie
Output : Enter the first string : Aaaaaaaaaaaaaaaaaaaa
So, I thought I should clear the buffer in the standard way, as in C. It waits until I enter
return
twice. The first time it is added to the line and the next time, expecting more input with a different return. 1. Is there any method by which I can flush the buffer without making any extra return? 2. How can I implement error handling for the same? Since the return value of fgets will be Non-null, and strlen (input) gives me the accepted string size of fgets, what should I do?
thanks a lot
source share