...
Oh. The placeholders basically tell scanf how to interpret the information it recieves from inp. Suppose you're running fscanf on the following input...
1234.567
fscanf (inp, "%c", &aChar);
would read the first ASCII character, '1'
fscanf (inp, "%d", &anInt);
would read the first int, which is 1234
fscanf (inp, "%f", &aFloat);
would read the first float (real #), which is 1234.567
So suppose I had to read a file that had a person's initials (HJ), his age (26), and how much cash he has in his wallet ($33.49). This is what the file would look like:
person.dat:
HJ 26 33.49
And this is what the code would look like:
Code:
main()
{
char First, Last; // first and last initials
int age;
float cash;
FILE*inp;
inp=fopen("person.dat","r");
fscanf(inp,"%c", &First); // First is a char, so I use %c
fscanf(inp,"%c", &Last); // Last is a char, so I use %c
fscanf(inp,"%d", &age); // age is an int, so I use %d
fscanf(inp,"%f", &cash); // cash is a float, so I use %f
}
Or I could just use one big fscanf command instead of the 4 little ones...
Code:
fscanf(inp,"%c%c%d%f", &First, &Last, &age, &cash);
Basically, fscanf isn't clever enough to recognize what kind of variable it's being given, so you have to tell it what kind of variable it's being given, using a placeholder.
And of course, make sure that the variable you tell it to write to is the same type as the placeholder specifies.
As a bit of trivia, the technocal term term for a placeholder is "conversion specifier", albeit a term that doesn't come up very much.