下面我的代码可能出错,它永远不会打开文件。我也试过绝对文件路径,但这对我没有帮助,我实际上知道该文件在那里。
FILE *myfile;
myfile= fopen("IN.txt",r);
if (myfile != NULL)
{
while ( fscanf(myfile,"%lf",&test) !=eof )
{
printf("%f",test);
printf("\n");
}
}
fclose(myfile);
答案 0 :(得分:3)
也许你想这样做:
myfile= fopen("IN.txt","r");
这是因为第二个参数是const char * type
在这里:
while ( fscanf(myfile,"%lf",&test) !=EOF )
(C区分大小写。)
编辑: 我想建议使用类似的东西:
while ( (fscanf(myfile, "%lf", &test)) > 0){...}
答案 1 :(得分:1)
尝试使用
打印错误printf(“打开文件时出错:%s \ n”,strerror(errno));
答案 2 :(得分:0)
myfile= fopen("IN.txt",r);
应该
myfile = fopen("IN.txt","r");
并确保您的文件系统与文件名所暗示的一样敏感(因此“IN.txt”在UN * X不同的文件中为“in.txt”)