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_value
s 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.
Solution 1Use fgets - C++ Reference[^] to read the text file line by line into the buffer. Then parse each line to get the column elements.
You need to parse the CSV file. My guess is that you can assume that none of the CSV fields contain a ',' or a newline. If so, then parsing is simple: just read line by line using std::getline into a std::string, then parse the string into tokens with ',' as the delimiter.
You can use fgets [^] in a loop to read a file line by line. When no more lines can be read, it will return NULL. On the first line you can use sscanf[^] to extract the integer.
Steps To Read A File: Open a file using the function fopen() and store the reference of the file in a FILE pointer. Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread(). File close the file using the function fclose().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With