input function question

a person randallr at
Wed Jun 2 19:53:11 PDT 2004

Does anyone know an input function in C that does a better job than fscanf?

In a program I wrote was the statement

	items=fscanf(file,"%d %d %d\n",&a,&b&,&c);

where a,b,c were pointers to integer variables.

This was intended to match an input database file of 3 numbers per line.


	34 39 57
	38 51 108
	41 62
	43 58 97

Now the program churned along and looped over the line, inputting 3 new values 
per each input line.  That is

	a,b,c = 34,39,57
	a,b,c = 38,51,108

but then the program jumped over the short line above and produced

	a,b,c = 41,62,43
	a,b,c = 58,97,...

and the values of items stayed = 3 all the time.

I naively expected the input format %d %d %d\n to match 3 and only 3 items per 
input line, and to output less than 3 if the input line was missing a number.

Needless to say, the UNIX manual for fscanf says that white spaces including 
new lines are ignored, when values are assigned to variables so I never saw 
items < 3, which led me to falsely conclude that all input lines were as 

What I need is a function that tries to match the variables to the format 
line, but returns items equal to the actual count on that line, up to the 
newline character. (it doesn't count the newline as whitespace)

That way if items is not equal to 3, then I know that I have an error on an 
input line in my database and can print an error message on the spot.

Does anyone know of an improved fscanf that does what I want above?

Thanks for your reply.

Sincerely, Randall

More information about the KPLUG-List mailing list