<<< split from fgets in while loop >>>
I have the same problem now, how to solve this?
Use another fgets to retrieve those "rubbish data" and ignore it? This way seems to be ugly.
<<< split from fgets in while loop >>>
I have the same problem now, how to solve this?
Use another fgets to retrieve those "rubbish data" and ignore it? This way seems to be ugly.
fgets() is designed to take a whole row of text, into one string char.
Are you trying to separate each word out of a line of text?
No.
The real problem is, I want to store user input into a char[], so I can allocate memory as char[10], I expect the user input data 10 bytes each row, and I will request the user input for 3 rows, however, what user input is not under my control, so if the user input 12 characters in the first line, then the extra 2 characters will be automatically read by the next fgets function call, this is not I expected.
I want to take the first 10 bytes from user input, and ignore the rest of user input in case the user does not follow the program rule(only 10 bytes in one row). And then request the user for another input, take the first 10 bytes and ignore the rest as well.
How to handle this?
Code:fgets(buf,11,stdin); if( strchr(buf,'\n') == NULL) { // no newline in buf int c; while( (c=getchar()) != EOF && c != '\n') ; // error("input longer than 10 chars"); }
Last edited by Bayint Naung; 10-29-2010 at 03:07 AM.
Thanks for your codes, but I don't want to validate the input and throw out an error message to terminate my program, I just want to ignore the useless user input(those characters beyond the first 10 bytes).
I know one solution could be that I make another fgets function call if I detect that user input is longer than what I need and simply ignore it. However, this still needs to validate the input and it also has some overhead to make a fgets function call that simply eat up those rubbish data, this way as what I said in the previous post, it is ugly.
I just wonder if there is any other ways.
Last edited by icoigo; 10-29-2010 at 03:52 AM.
This is the way I'd do it:
If the user is going to mess with you, beyond a 100 char's per line, let me know - we'll go "industrial" strength on 'em!Code:#include <stdio.h> int main() { int i,j; char str[3][10+1]; char line[100]; printf("\n\n\n"); for(i=0;i<3;i++) { printf("\nEnter a line of text: "); fgets(line, sizeof(line), stdin); line[10]='\0'; strcpy(str[i], line); line[0]='\0'; //purely defensive } for(i=0;i<3;i++) printf("\nstr[%d]: %s", i+1, str[i]); printf("\n\n\t\t\t press enter when ready"); (void) getchar(); return 0; }
> The real problem is, I want to store user input into a char[], so I can allocate memory as char[10],
The real problem is you're assuming that the user enters correct data.
The actual steps you should be performing are:
- read the user input into a temporary buffer, taking care of I/O errors.
- validate the input (length, content, whatever)
- store the valid input where you want to keep it.
This is normally achieved by declaring the temporary buffer to be BUFSIZ in length.
If all you want is the first 10 chars, then that's an easy strncpy().
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.