Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fscanf(....) question.

Status
Not open for further replies.

powerb23

Technical User
Sep 11, 2003
11
0
0
US
Hi,
I would like to write an fscanf statement to read the first integer value at the start of the line.This is what i was using:

int c = 0;

int sr = fscanf(inFile, "%d", &c);

I was puzzled as to why this returned a value of 1 when the line contained:

8as 12


Your help will be appreciated.
 
doesnt fscanf return the number of fields successfully read ? in your case, 8 would have been successfully read into c and you get 1 in sr.
 
> I was puzzled as to why this returned a value of 1 when the line contained:
Which begs the question "what answer were you expecting"?

Given
int sr = fscanf(inFile, "%d", &c);
And a file containing
8as 12

sr would be 1 (ie, an integer was found, converted and assigned)
c would be 8 (the result of the conversion)
'a' would be the next character to be read from the input file.

--
 
Thanks for all your help. I now understand what happens

Much thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top