普通C从数据文件中读取

时间:2014-06-01 20:08:29

标签: c gcc file-io scanf

我在Ubuntu 12.04.4 LTS 32位上使用编译器 gcc(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3 ,并使用fscanf函数从文件读取有点问题。格式说明符%f无法读入代码中声明为双精度的变量,但%lf有效。这种差异背后的原因是什么?我提供了一个代码片段以及下面的数据文件:

编辑:我在使用以下代码段的较大代码中遇到分段错误。我已经尝试删除前两行并删除了char和两个fgets调用,但它没有用。

#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;
}

编辑:更正了循环

#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

顺便说一下,如果不删除数据文件中的两个标题行,是否可以从数据文件的第三行开始读取?

提前致谢

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

如果您正在阅读双变量,则需要%lf。 scanf不是类型安全的;你必须通过仔细匹配变量类型的格式说明符来帮助该函数。 %ld等同样的事情。