读取包含二维数据的文件并将它们存储到数组中(这是我第一次使用 C)


int main() {
    int i;int j;char c;double num;
    double point[65][2];
    FILE *myfile;
    myfile = fopen("point.txt","r");
    if(myfile == NULL)
    {
        printf("can not open filen");
    }
    else{
        for(i=0;i<65;i++){
            for (j=0; j<2; j++) {
                fscanf(myfile, "%lf", &num);
                point[i][j]=num;
                printf("%lf",point[i][j]);
            }
        }
    }
    fclose(myfile);
}

我不知道为什么它总是给我一个空数组。数据中有 65 个观测值。这就是我创建一个 65x2 阵列的原因。

该文件如下所示:

1.87046225914495 0.37205807606083

1.51453361512525 0.45942874936008

..

..

我看不到此代码的任何错误。它可以稍微改变一下:

  • 您可以直接调用目标值 ( fscanf(myfile, "%lf", &(point[i][j])); ( 上的fscanf
  • 如果您的文件格式始终为 X*2 值,您可以删除第二个 for 循环并读取两个值 ( fscanf(myfile, "%lf%lf", &(point[i][0]), &(point[i][1])); (
  • 您应该在阅读后打印表格内容,以便确保打印最终值(例如,如果您在某一点覆盖了一些值(

我唯一的猜测与文件格式有关。如果scanf无法读取值(没有足够的值,格式不正确的文件...(,您将无法获得结果。

如@ameyCU所述,您必须检查返回值fscanf。它返回成功读取的"令牌"数。您应该检查它是否真的是==1,否则这意味着格式不好(或文件中没有可用数据(。在这种情况下,部分或全部表的内容是未定义的(并且根据操作系统可以填充0(。

编辑:如果此文件中确实有 65x2 值,请检查无效字符,即如果您使用 Word 或类似内容来创建数据文件。

这是您问题的解决方案,请尝试阅读和理解代码。基本上,你的想法是正确的,但我纠正了你犯错的代码。解决方案的主要工作是在while()循环中。所以通过它。

#include<stdio.h>
//#pragma warning(disable : 4996)

int main() 
{
         int i=0, in = 0; 
         int j=0, jn = 0; 
         int numberOfCol = 0;
         double num1,num2;
         double point[65][2];
         FILE *myfile=NULL;
         myfile = fopen("test.txt", "r");
         if (myfile == NULL)
         {
            printf("can not open filen");
         }
         else 
         {
           while (!feof(myfile))
           {
               fscanf(myfile, "%lf %lf", &num1, &num2);//since your file contains two numbers per row you need two variables to store the values
               point[i][j] = num1;//At this point (i=0,j=0) you will get the first value of in the row/line. 
               printf("%lf ", point[i][j]);
               ++j;//incremet j so that you move the index to next column i.e (i=0,j=1)
               point[i][j] = num2;//store the second column value of the row/line
               numberOfCol++;
               printf("%lf", point[i][j]);
               printf("n");
               ++i;//go to next row/line 
               j = 0;//set back the column index to 0 aka the first column index            
            }//Repeat this process till you reach End of file
          }
    fclose(myfile);
    printf("File Data..n");
    for (in = 0; in<i; in++) 
    {
       for (jn = 0; jn<numberOfCol; jn++)
       {
          printf("%lf ",point[in][jn]);
       }
       printf("n");
    }
    return 0;
}

编辑:这是一个修改后的代码,由hexasoft在注释中建议的检查条件组成。谢谢海克斯软

#include<stdio.h>
//#pragma warning(disable : 4996)

int main() 
{
    int i=0, in = 0; 
    int j=0, jn = 0; 
    int numberOfCol = 0;
    double num1,num2;
    double point[65][2];
    int result;
    FILE *myfile=NULL;
     myfile = fopen("test.txt", "r");
     if (myfile == NULL)
     {
        printf("can not open filen");
     }
     else 
     {
        while (i<65)//65 or how may ever rows/lines of data you have in your file
        {
            result=fscanf(myfile, "%lf %lf", &num1, &num2);
            if (result == 2)
            {
                point[i][j] = num1;
                printf("%lf ", point[i][j]);
                ++j;
                numberOfCol = j+1;
                point[i][j] = num2;
                printf("%lf", point[i][j]);
                printf("n");
                ++i;
                j = 0;
            }
            else
            {
               printf("Format Error!n");
            }
            if (feof(myfile))
            {
                printf("End of file reached!n");
                break;
            }
    }
}
fclose(myfile);
printf("File Data..n");
for (in = 0; in<i; in++) 
{
    for (jn = 0; jn<numberOfCol; jn++) 
    {
        printf("%lf ",point[in][jn]);
    }
    printf("n");
}
  return 0;
}

最新更新