#include /* files3.c 1/24/96 Since scanf() and fscanf() sometimes read past the newline character in search of a string or an integer, you can get into trouble if you don't want that to happen. There's an alternative way of doing one-line-at-a-time input without giving up the power of scanf(). 1. First, let's look at the return value of scanf()/fscanf(). The return value is the number of successful matches scanf() made. Try the program below with "program < textfile" while varying the contents of the textfile. The call to scanf() is expecting the input to contain an integer, some optional white space, a string, some more optional white space, and then another string. If that's what you put in "textfile", what happens with "matches"? What if you don't start with an integer, or only put one string after the integer, or....? 2. Put another scanf( "%s", aString ); printf( "@%s@\n", aString ); after the stuff below. If "textfile"'s first line contains an integer and one string, and its second line contains something, how does your code behave? Did scanf() go past the first newline in search of a string, or not? 3. Sometimes you don't want scanf() to look past the current line. One solution is to get the current line into an array, like this: char line[100]; int i, matches; char aString[50], anotherString[50]; gets( line, 100 ); matches = sscanf( line, "%d %s %s", &i, aString, anotherString ); "sscanf()" takes its input from a string rather than from a file (or stdin, which is a kind of file). 4. Here's the big question. If you do scanf( "%d", &i ); scanf( "%s", aString ); instead of scanf( "%d %s", &i, aString ); what happens? If you do gets( line, 100 ); sscanf( line, "%d", &i ); sscanf( line, "%s", aString ); instead of gets( line, 100 ); sscanf( line, "%d %s", &i, aString ); what happens now? */ void main() { int i, matches; char aString[100], anotherString[100]; matches = scanf( "%d %s %s", &i, aString, anotherString ); printf( "Successful matches: %d\n", matches ); printf( "%d @%s@ @%s@\n", i, aString, anotherString ); }