【问题标题】:Storing Double Floating Point Numbers from a .dat file into a 1-D Array将 .dat 文件中的双浮点数存储到一维数组中
【发布时间】:2016-03-07 18:20:14
【问题描述】:

所以我在存储 .dat 文件中的一些数字时遇到了一些困难。文件如下:

1
2
7
10
9
4
0
5
6
8
3

我必须尝试检索此信息的代码是:

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

#define N 11

int main(int argc, char** argv) {

    //Declare variables: number of elements, counter, array, & valuesto
    //print
    int i;
    double a[N], num, temp;
    FILE* dat;

    //Print space for program cleanliness
    printf("\n");

    //Open file
    dat = fopen("zero.dat", "r");      

    //Initialize i  
    i=0;

    //Read in infromation from file
    while(!feof(dat) && i < N){
        printf("This is loop number %d\n", i);
        num = fscanf(dat,"%lf", &temp);
        printf("Temp variable is stored as: %f\n", temp);
        printf("Number of characters read in: %d\n\n", num);
        a[i] = temp;
        i++;
    }        

    fclose(dat);    

    for(int j=0;j<i;j++){

        printf("%f\n", a[j]);

    }

    //Exit program
    exit(EXIT_SUCCESS);
}
/**************************************************************************/

输出是:

This is loop number 0
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 1
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 2
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 3
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 4
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 5
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 6
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 7
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 8
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 9
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

This is loop number 10
Temp variable is stored as: 0.000000
Number of characters read in: 1541763072

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

另外,需要注意的是我使用的是 NetBeans IDE 8.0.2; zero.dat 文件与源文件位于同一项目文件夹中。

【问题讨论】:

  • 您的num 是双精度数,但您将其打印为整数。 fscanf的返回值是一个int,但它不返回扫描的字符数,而是格式转换的次数或特殊值EOF表示已经到达文件末尾。
  • 总是检查fopen();的结果
  • 如果我将num 更改为int,您的示例对我有用。您应该检查文件是否可以像 Peter Miehle 指出的那样打开。可能导致转换错误的另一件事是文件编码。例如,如果您的文件有 BOM(字节顺序标记),fscan 会尝试从中转换双精度,但失败了。因为失败的转换会将文件指针返回到它的旧位置,所以所有的转换都会失败。尝试将文件保存为没有 BOM 的纯 ASCII 或 UTF-8。
  • 您可以对文件进行十六进制转储,以查看文件开头是否有任何“奇怪”字符。如果是这样,请在文本编辑器中更改编码并再次保存文件。

标签: c arrays file-io scanf


【解决方案1】:

您的代码打印 Number of characters read in: 1541763072 因为 fscanf 返回一个 int 并且您将它收集在一个双变量中,不知道为什么要放置垃圾值,但将其更改为类似这样的 int 变量

int j;
j=fscanf(dat,"%lf", &temp);
printf("Number of characters read in: %d\n\n", j);

检查文件是否存在是一个很好的编程习惯。如果文件不存在,您只会收到分段错误,并且您不会知道它为什么返回分段错误。而是处理它

dat = fopen("zero.dat", "r");
if(dat == NULL)
{
     //your message
     return;
}

【讨论】:

  • 我加了这个条件格式,运行还是一样,提示文件IS打开了;但是,它现在打印零而不是随机垃圾。所以似乎fscanf 没有正确读取,但我不知道为什么......
  • 试试fgets 看看数据是否至少可以访问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多