【问题标题】:How to read these mixture of data in C如何在 C 中读取这些混合数据
【发布时间】:2016-06-15 12:03:37
【问题描述】:

我必须将每个数据存储在一个数组中。如何从文件中读取这些数据?

120 5.0000000000000000E-01   -5.0000000000000000E-01  5.0000000000000000E-01  
5.0000000000000000E-01   -5.0000000000000000E-01  -5.0000000000000000E-01 
5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01  
5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01 
-5.0000000000000000E-01

数据是整数、浮点数和指数的混合体。连续数据之间的空间不是恒定的,因此我不能使用直接的 fscanf()。我还必须将它们更改为整数,因此我找不到 fscanf() 的替代方法,因为我可以在 fscanf() 参数中将类型说明符指定为 %e,然后将它们更改为整数。我也试过 fgetc() 。请告诉我一个方法。

编辑。

要使用 fscanf(),我需要在连续数据之间有恒定数量的空格或逗号或任何内容。这里,每个数据之间的空格数不是恒定的。所以,我还需要在两者之间实现一个空间检查器。这就是我在其中使用 fgetc() 的原因。

#include<stdio.h>

int main()
{
int i=0,c,a[13];
FILE *fp;
fp=fopen("test.txt","r");
if(fp==NULL)
{
    printf("Error");
}
else
{
    i=0;
    while(1)
    {
        c=fgetc(fp);
        //printf("\nc = %c",c);
        if(feof(fp))
        {
            break;
        }
        else if(c!=" ")
        {
            fscanf(fp,"%d",&a[i]);
            printf("%d\n",a[i]);
            i++;
        }
    }

}
fclose(fp);
return 0;
}

数据文件为 2 m.b。我刚刚发布了其中的一部分。其他部分有浮点数,不像这里那样是指数的。

【问题讨论】:

  • Please give me a code....改为投反对票。
  • 欢迎来到 Stack Overflow!请展示您的研究成果。请先阅读How to Ask页面。
  • 请给我很多钱,然后我会给你很多代码。
  • 为什么不能使用“直截了当fscanf()”?请注意,e 格式化指令对所有样式的浮点数都是正确的。
  • 您显示的输入中似乎只有一个整数,并且所有浮点数的格式都相同。所以我真的不明白问题是什么。请告诉我们您尝试了什么,编辑您的问题并将相关代码复制粘贴到问题正文中,详细说明哪些有效的广告不适用于您的节目代码。

标签: c scanf fgetc


【解决方案1】:

此程序从文件中读取并将标记转换为浮点数。您应该阅读整行并将其标记化。然后你只需要创建一个数组并在代码中的正确位置添加到数组中。

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

int main(void) {
    FILE *sp1;
    char line[256];
    double array[256];
    int i = 0;

    sp1 = fopen("data.txt", "r");
    while (1) {
        if (fgets(line, 150, sp1) == NULL) break;
        char *p = strtok(line, " ");
        while (p != NULL) {
            double fd = atof(p);
            array[i++] = fd;
            p = strtok(NULL, " ");
        }
    }

    for(int j=0;j<i;j++)
        printf("%f\n", array[j]);

    return 0;
}

data.txt

120 5.0000000000000000E-01   -5.0000000000000000E-01  5.0000000000000000E-01
5.0000000000000000E-01   -5.0000000000000000E-01  -5.0000000000000000E-01
5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01
5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01
-5.0000000000000000E-01

输出

120.000000
0.500000
-0.500000
0.500000
0.500000
-0.500000
-0.500000
0.500000
-0.500000
0.166667
0.500000
-0.500000
-0.166667
-0.500000

【讨论】:

    【解决方案2】:

    试试下面的代码,它将从输入文件中读取所有数字作为双精度数。

    数字之间的空格无关紧要

    #include<stdio.h>
    #include<stdlib.h>
    
    int main(){
    
    
        FILE * fp;
        double num;
    
        fp = fopen("input.txt", "r");
    
        if (fp == NULL){
            printf("Unable to open file, terminating ...");
            exit(1);
        }
        else {
            while( fscanf(fp, "%lf", &num) == 1){
                printf("%lf %e\n", num, num);
            }
            fclose(fp);
        }
        return 0;
    }
    

    input.txt:(在你的文件中添加了更多的空格和数字)

    120 5.0000000000000000E-01     -5.0000000000000000E-01  5.0000000000000000E-01
    
    5.0000000000000000E-01  -5.0000000000000000E-01                 -5.0000000000000000E-01
    
    5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01
    5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01   17.3 16.55511
    
    
    -5.0000000000000000E-01
    

    输出:

    120.000000 1.200000e+02
    0.500000 5.000000e-01
    -0.500000 -5.000000e-01
    0.500000 5.000000e-01
    0.500000 5.000000e-01
    -0.500000 -5.000000e-01
    -0.500000 -5.000000e-01
    0.500000 5.000000e-01
    -0.500000 -5.000000e-01
    0.166667 1.666667e-01
    0.500000 5.000000e-01
    -0.500000 -5.000000e-01
    -0.166667 -1.666667e-01
    17.300000 1.730000e+01
    16.555110 1.655511e+01
    -0.500000 -5.000000e-01
    

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多