c - sscanf() double showing zeros -
i'm having troubles sscanf() function read doubles. have comma separated text file this:
abc,def,0.465798,0.754314 ghi,jkl,0.784613,0.135264 mno,opq,0.489614,0.745812 etc.
so first line fgets() , use sscanf() 2 string , 2 double variables.
fgets(buffer, 28, file); sscanf(buffer, "%4[^,],%4[^,],%lf[^,],%lf[^\n]", string1, string2, &double1, &double2); printf("%s %s %f %f\n", string1, string2, double1, double2);
but output is:
abc def 0.465798 0.000000 ghi jkl 0.784613 0.000000 mno opq 0.489614 0.000000
so somehow doesn't scan last float. i've tried %lf[^ \t\n\r\v\f,]
, %lf
still doesn't work.
change
"%4[^,],%4[^,],%lf[^,],%lf[^\n]"
to
int result; result = sscanf(buffer, "%4[^,],%4[^,],%lf,%lf", string1, string2, &double1, &double2); if (result != 4) // handle error
notice &
on double1
, double2
- likley typo.
also strongly recommend check result 4. not checking sscanf()
result core question. "zeros" printed out result of double2
not having been scanned , retained previous value have been anything. had sscanf()
result been checked have reported 3, showing problem between scanning double1
, double2
. in larger scene, practice verify all expected values scanned before going on.
Comments
Post a Comment