Do you think the fgets and sscanf combination is also a right candidate for non-user-interactive input, e.g. file input? Which functions should be used for file input? Thank you. It depends on the "provenance " of the file. It's perfectly all right to use fscanf() directly if you...
C users must avoid using dangerous functions that do not check bounds unless they've ensured that the bounds will never get exceeded. Functions to avoid in most cases (or ensure protection) include the functions strcpy(3), strcat(3), sprintf(3) (with cousin vsprintf(3)), and gets(3). ...
With the scenario above as background, we can now answer your question, “Why would you possibly want to discard the user's input?” For better or worse, many beginning programmers usescanfto read numbers andgetsto read strings. This is in large part, of course, because these functions ar...
> > Now what I have to do is, that I have to read these data from the file > > in sequence from left to right.[/color] > > This should be possible with fopen() and fread().[/color] It's text in a text file. I'd use fgets() followed by sscanf(), or quite possibly (...
When you use functions to read strings or single characters from the keyboard, eg. fgets() and getchar(), they will store the ASCII values of the input. Therefore, it is important to remember that a 7 from the user isn't a 7 within your program. Here is an example program to show...
It's not defined in Standard C and if it is C++'s std::string you might prefer to post in comp.lang.c++. int n_mines; int i; int good_input = 1; > printf("How many mines do you want in the minefield?"); fgets(input, sizeof(input), stdin); > for(i = 0; i < strlen...
if so, use fgets() to read the line, then read up on sscanf() and strchr(). Mark McIntyre -- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." ...
A much better approach is to use fgets() to read a line at a time, then use sscanf() to scan the resulting line. sscanf(), unlike fscanf(), doesn't consume its input; it's still there in the string, and if the sscanf() call fails (check its returned value), you can try ...