User1291
User1291

Reputation: 8181

c - reading a specific column in data file

So I created a data file like so:

for(size_t n = ...;...;...){
    //do some stuff
    double mean_value = ...
    double min_value = ...
    double max_value = ...

    FILE *fp = fopen(OUTPUT_FILE,"a+");
    fprintf(fp,"%d %lf %lf %lf\n",n,mean_value, min_value, max_value);
    fclose(fp);
}

And now i want to read the mean_values that I've written...

FILE *fp = fopen(OUTPUT_FILE,"a+");
double *means = malloc(...);
for(size_t i = 0; ...; ...){
    fscanf(fp,"%*d %lf %*lf %*lf\n", &means[i]);
}
//more stuff
fprintf(fp,...);
fclose(fp);

And gcc complains about that:

warning: use of assignment suppression and length modifier together in gnu_scanf format [-Wformat=]

fscanf(fp,"%*d %lf %*lf %*lf\n", &means[i]);

         ^

And I'm not sure what it's trying to tell me, here.

Upvotes: 14

Views: 4069

Answers (2)

jo_
jo_

Reputation: 9270

So this

fscanf(fp,"%*d %lf %*lf %*lf\n", &means[i]);

says

  • read decimal drop it
  • read float assign to long-float given pointer
  • read float drop it (no use to tell holder size since we don't use it)
  • read float drop it (no use to tell holder size since we don't use it)

this will do the job (no useless size precision)

fscanf(fp,"%*d %lf %*f %*f\n", &means[i]);

Upvotes: 0

Eugene Sh.
Eugene Sh.

Reputation: 18331

The length specifier (namely l in lf) in the format string is intended to indicate the size of the receiving parameter in case it is assigned, while f tells how the input should look like. It means that specifying the length for the fields which are suppressed is meaningless, and your compiler is just trying to make sure you haven't mistakenly typed * instead of %. Just remove the l from the suppressed fields.

Upvotes: 22

Related Questions