简单问题。
当我尝试打开一个名为text.txt的文件时,它可以正常工作。
但是,如果我将文件重命名为text.cir.txt,就会出现错误。
我能做些什么来修复它?
FILE *fd;
char nome_fich[] = "text.cir.txt";
int x;
fd = fopen("text.cir.txt", "r");
if (fd == NULL)
{
printf("ERROR");
}
else
{
while ((x = fgetc(fd)) != EOF)
{
printf("%c", x);
}
fclose(fd);
}
以下建议的代码:
- 干净地编译
- 执行所需的功能
- 正确检查和处理错误
现在,提出的代码:
#include <stdio.h> // FILE, fopen(), perror(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
int main( void )
{
FILE *fd = fopen( "text.cir.txt", "r" );
if ( !fd )
{
perror( "fopen failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
int x;
while ((x = fgetc(fd)) != EOF)
{
printf("%c", x);
}
fclose(fd);
}
当针对任何.txt文件运行时,它将执行所需的操作。
注意:我运行的是Linux版本18.04