Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c - reading a specific column in data file

Tags:

c

file

io

scanf

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.

like image 823
User1291 Avatar asked Mar 09 '16 19:03

User1291


People also ask

How do you read a specific column from a file in C?

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.

How do I read a specific column in a CSV file in C ++?

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.

How do you read a specific line in a file C?

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.

What is the syntax to read the data from files in C?

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().


1 Answers

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.

like image 50
Eugene Sh. Avatar answered Sep 28 '22 00:09

Eugene Sh.