读取包含二维数据的文件并将其存储到数组中(这是我第一次使用c) intmain(){ int i;int j;字符c;双数字; 双点[65][2];; 文件*myfile; myfile=fopen(“point.txt”、“r”); if(myfile==NULL) { printf(“无法打开文件\n”); } 否则{ 对于(i=0;i
我看不到此代码上有任何错误。可以稍微更改它:读取包含二维数据的文件并将其存储到数组中(这是我第一次使用c) intmain(){ int i;int j;字符c;双数字; 双点[65][2];; 文件*myfile; myfile=fopen(“point.txt”、“r”); if(myfile==NULL) { printf(“无法打开文件\n”); } 否则{ 对于(i=0;i,c,arrays,C,Arrays,我看不到此代码上有任何错误。可以稍微更改它: 您可以直接调用目标值(fscanf(myfile,%lf,&(点[i][j]);) 如果文件格式始终为X*2值,则可以删除循环的第二个,并读取这两个值(fscanf(myfile,“%lf%lf”,&(点[i][0]),&(点[i][1]);) 您应该在阅读后打印表格内容,以便确保打印最终值(如果您在某一点覆盖了某些值) 我唯一的猜测是与文件格式有关。如果scanf读取值失败(值不够,文件格式错误…),则不会得到结果 如@ameyCU所述,您必须
- 您可以直接调用目标值(
)fscanf(myfile,%lf,&(点[i][j]);
- 如果文件格式始终为X*2值,则可以删除循环的第二个
,并读取这两个值(
)fscanf(myfile,“%lf%lf”,&(点[i][0]),&(点[i][1]);
- 您应该在阅读后打印表格内容,以便确保打印最终值(如果您在某一点覆盖了某些值)
scanf
读取值失败(值不够,文件格式错误…),则不会得到结果
如@ameyCU所述,您必须检查fscanf
返回值。它返回成功读取的“令牌”数。您应该检查它是否真的==1,否则表示格式不好(或文件中没有可用数据)。在这种情况下,表的部分或全部内容未定义(取决于操作系统,可以用0填充)
编辑:如果此文件中确实有65x2值,请检查是否存在无效字符,即如果您使用Word或类似工具创建数据文件。以下是解决问题的方法,请尝试阅读并理解代码。基本上,您的想法是正确的,但我在您出错的地方更正了代码。解决方案的主要工作是ion在
while()
循环中。所以请检查它
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 file\n");
}
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);
}
#包括
//#杂注警告(禁用:4996)
int main()
{
int i=0,in=0;
int j=0,jn=0;
int numberOfCol=0;
双num1,num2;
双点[65][2];;
FILE*myfile=NULL;
myfile=fopen(“test.txt”、“r”);
if(myfile==NULL)
{
printf(“无法打开文件\n”);
}
其他的
{
而(!feof(myfile))
{
fscanf(myfile,“%lf%lf”、&num1、&num2);//由于文件每行包含两个数字,因此需要两个变量来存储值
点[i][j]=num1;//此时(i=0,j=0),您将获得行/行中的第一个值。
printf(“%lf”,点[i][j]);
++j、 //增加j,以便将索引移动到下一列,即(i=0,j=1)
点[i][j]=num2;//存储行/行的第二列值
numberOfCol++;
printf(“%lf”,点[i][j]);
printf(“\n”);
++i、 //转到下一行/行
j=0;//将列索引设置为0,即第一列索引
}//重复此过程,直到到达文件末尾
}
fclose(myfile);
printf(“文件数据..\n”);
对于(in=0;in),您应该检查fscanf
的return
。还请显示文件的格式。您可以发布point.txt吗?我刚刚将65行文件更改为14行文件,效果很好。我的point.txt文件:“它给我一个空数组”是什么意思?无论您是否执行printf
操作,都会打印一个值(垃圾或0s)。我用这些值填充了一个文件(重复了65次)并运行此代码,它工作正常。正如我在回答中所说的,检查fscanf
返回值(它真的总是返回1吗?)。如果没有,请检查您的文件(也就是说,它真的是一个文本文件吗?没有控制字符吗?它是否来自文本编辑器,而不是Word之类的文档编辑器?)。好吧,我不知道你找到了什么“问题的解决方案”。你的while
如果可以,但如果输入文件中有足够的数据,它不会改变任何东西(与for
循环相比)。更糟糕的是,如果存在超过65x2的数据,您将在点
结束(so UB)后插入数据。检查feof
是不够的,因为它是在读取失败后引发的。因为您没有检查fscanf
返回值,您将有未定义的值。@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];
FILE *myfile=NULL;
myfile = fopen("test.txt", "r");
if (myfile == NULL)
{
printf("can not open file\n");
}
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;
}
#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 file\n");
}
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;
}