C - fscanf Mixed numbers and static text

I am trying to read a file formatted like this:

Text Description: 12
Description2: 1
More descriptive things: 6

And I would like to read the numbers 12, 1 and 6 in variables.

I tried code like this:

fscanf(fptr, "Text Description:%d",&desc1);
fscanf(fptr, "Description2:%d",&desc2);
fscanf(fptr, "More descriptive things:%d",&desc3);

But for some reason, only the first variable is populated. Does anyone know why this is so?

+5
source share
2 answers

Add a space at the beginning of the string format to avoid problems with a new line

fscanf(fptr, " Text Description:%d",&desc1);
fscanf(fptr, " Description2:%d",&desc2);
fscanf(fptr, " More descriptive things:%d",&desc3);
+1
source

12, , , , . fscanf ( , , ). fgetc, ,

fscanf(fptr, "Text Description:%d",&desc1);
fgetc(fptr); // drop the next character
fscanf(fptr, "Description2:%d",&desc2);

'\n' :

while (fgetc(fptr) != '\n')
   ;
0

All Articles