Reading in a specific column of data from a text file in C

前端 未结 4 989
说谎
说谎 2020-12-04 02:56

My text file looks like this:

987 10.50   N   50
383 9.500   N   20
224 12.00   N   40

I want to read only the second column of data. How w

4条回答
  •  不思量自难忘°
    2020-12-04 03:35

    You'll need to read all the data, and discard the unneeded fields (i.e. "columns"). Format strings containing %*d are doing that.

    In C, it could be something like (assuming f is a FILE* handle)

     while (!feof(f)) {
        int n=0; double x=0.0; char c[4]; int p=0;
        if (fscanf(f, " %*d %f %*[A-Z] %*d",  &x) < 1)
          break;
        do_something(x);
     }
    

    PS. Thanks to Jerry Coffin for his comment

提交回复
热议问题