scanf - fscanf usage in C -
i have file this:
10 15
i want read tree variables, let's number1, number2, , mystring. have doubts kind of pattern give fscanf. thinking this;
fscanf(fp,"%i %i\n%s",number1,number2,mystring);
should work, , also, correct way of reading file? if not, suggest?
read each line fgets
(or getline
if have it), split line strsep
(better, if available) or strtok_r
(more awkward api more portable), , use strtoul
convert strings numbers necessary.
*scanf
should never used, because:
- some format strings (e.g. bare
"%s"
) eager overflow buffersgets
is. - behavior on integer overflow undefined -- invalid input can potentially crash program.
- they not report character position of first scan error, making nigh-impossible recover parse error. (this can mitigated using
fgets
,sscanf
instead offscanf
.)
Comments
Post a Comment