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:

  1. some format strings (e.g. bare "%s") eager overflow buffers gets is.
  2. behavior on integer overflow undefined -- invalid input can potentially crash program.
  3. they not report character position of first scan error, making nigh-impossible recover parse error. (this can mitigated using fgets , sscanf instead of fscanf.)

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -