简体   繁体   中英

Plain C reading from a data file

I am using the compiler gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 on Ubuntu 12.04.4 LTS 32-bit and reading from a file using fscanf function is a bit problematic. The format specifier %f cannot read into variables declared as doubles in the code but %lf works. What is the reason behind this discrepeancy? I am providing a code snippet together with a data file below:

EDIT: I am getting segmentation fault in the larger code where the following code snippet is used. I have tried removing the first two lines and getting rid of char and two fgets calls but it did not work.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#define N 3500
int main(void)
{
    FILE * data = fopen("data_1657.dta", "r"), * fft = fopen("fftq3.dat", "w+");
    if(!data) // Checking if the file opened succesfully
        return 1;
    fftw_complex *in, *out;
    fftw_plan forward;
    forward = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    double *t, *ft, *err;
    int i = 0;
    t = (double *) malloc(sizeof(double) * N);
    ft = (double *) malloc(sizeof(double) * N);
    err = (double *) malloc(sizeof(double) * N);
    char str [100];
    fgets(str, 100, data);
    fgets(str, 100, data);
    while (fscanf(data, "%lf    %lf     %lf", &t[i], &ft[i], &err[i]) == 3)
    {
        printf("%lf \t %lf \n", t[i], ft[i]);
        i++;
    }


    fftw_execute(forward);
    fclose(data);
    fclose(fft);
    fftw_destroy_plan(forward);
    fftw_free(in);
    fftw_free(out);
    free(t);
    free(ft);
    return 0;
}

EDIT: Corrected loop

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    FILE * data = fopen("data_1657.dta", "r");
    double t, count, err;
while (fscanf(data, "%lf %lf %lf", &t, &count, &err) == 3)
{
    printf("%f \t %f \t %f \n", t, count, err);
}   


    return 0;
}




  time in seconds       count rate(c/s)  error
   ---------------------    --------      -------
    281.3251281976699829     115.639      21.228
    281.8250962048768997     149.639      22.774
    282.3250641971826553     111.639      21.039
    282.8250322043895721     155.639      23.036
    283.3250001966953278     141.639      22.420
    283.8249682039022446     107.639      20.848
    284.3249361962080002     135.639      22.151
    284.8249042034149170     151.639      22.861
    285.3248721957206726     127.639      21.786
    285.8248402029275894     143.639      22.509
    286.3248081952333450     129.639      21.878
    286.8247760981321335     123.639      21.602
    287.3247441053390503      93.639      20.166
    287.8247120976448059      95.639      20.264
    288.3246801048517227     131.639      21.969
    288.8246480971574783     127.639      21.786
    289.3246161043643951     107.639      20.848
    289.8245840966701508      91.639      20.066
    290.3245521038770676      77.639      19.356
    290.8245200961828232     115.639      21.228
    291.3244881033897400     109.639      20.944
    291.8244560956954956     125.639      21.694
    292.3244241029024124     107.639      20.848
    292.8243920952081680     105.639      20.752
    293.3243599981069565     133.639      22.060
    293.8243280053138733     183.639      24.221
    294.3242959976196289     161.639      23.295
    294.8242640048265457     141.639      22.420

By the way without deleting the two header lines in the data file is it possible to begin reading from the third row in the data file?

Thanks in advance

(f)scanf()看到%lf它期望指向double的指针(通常为64位) ,而%f则仅期望指向float的指针(通常为32位 )。

scanf: %f — float; %lf — double; %Lf — long double

You need %lf if you are reading into double variables. scanf is not type safe; you have to help the function by matching format specifiers carefully with variable types. Same thing for %ld etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM