为什么我不能从我的 C 程序中读取这个包含数字数据的文本文件?

Why I cannot read this text file with numerical data from my C program?

我有一个包含 4096 个数字数据的文本文件,我正试图用 C 源代码读取它。但是,我的程序根本不起作用。只显示“打开文件失败”

为什么?

但是,如果我尝试另一个文本文件,它会起作用。

请告诉我问题是什么!

来自两个文本文件的标题如下:

*cannot_read.txt

的头
   50129.248000000000     
   50129.248000000000     
   2193.2479999999996     
   2193.2479999999996     
   2961.2479999999996     
   2577.2479999999996     
   1809.2479999999996     
   81.247999999999593     
   721.24799999999959     
   1745.2479999999996     

*can_read.txt

3.45654675443
1.23536565353
123123.353535
3.45654675443
1.23536565353
123123.353535
3.45654675443
1.23536565353
123123.353535
3.45654675443

我的程序。

#include <stdio.h>

int main(void)
{
    int n, i;
    int j;
//    float fval[4097];
    long double fval[4097];

    FILE * fp;
//           fp = fopen("/home/changwan/C/can_read.txt","r");
             fp = fopen("/home/changwan/C/cannot_read.txt","r");

    if(fp==NULL){
            puts("fail to open a file!");
            return -1;
    }

   n = 0;
//   while (fscanf(fp, "%f", &fval[n++]) !=EOF)
   while (fscanf(fp, "%Lf", &fval[n++]) !=EOF)
       ;

   for (i=0; i<n-1; i++)
       printf("fval[%d]=%Lf\n", i, fval[i]);

   fclose(fp);
   return 0;
}

fopen()函数不支持~符号。查看realpath()函数以及绝对路径和相对路径的区别。