繁体   English   中英

从数据文件中进行纯C读取

[英]Plain C reading from a data file

我在32位Ubuntu 12.04.4 LTS上使用编译器gcc(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3 ,并且使用fscanf函数从文件读取有点问题。 格式说明符%f无法读入代码中声明为double的变量,但%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

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

提前致谢

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

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

如果要读入双变量,则需要%lf scanf类型不安全; 您必须通过将格式说明符与变量类型仔细匹配来帮助该功能。 %ld等也是如此

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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