Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - fscanf Mixed Numbers and Static Text

Tags:

c

scanf

I am trying to read a file formatted in this general way:

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

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

I have 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 being populated. Does anyone know why this is the case?

like image 771
Nathan Tornquist Avatar asked Nov 12 '22 09:11

Nathan Tornquist


1 Answers

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

fscanf(fptr, " Text Description:%d",&desc1);
fscanf(fptr, " Description2:%d",&desc2);
fscanf(fptr, " More descriptive things:%d",&desc3);
like image 123
MOHAMED Avatar answered Nov 15 '22 05:11

MOHAMED