带多个句点的C文件名
简单的问题 当我尝试打开一个名为text.txt的文件时,它工作正常 但是,如果我将文件重命名为text.cir.txt,则会出现错误 我能做些什么来修复它带多个句点的C文件名,c,fopen,C,Fopen,简单的问题 当我尝试打开一个名为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)
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);
}
#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);
}
#包括//文件、fopen()、peror()、printf()
#包括//退出(),退出失败
内部主(空)
{
文件*fd=fopen(“text.cir.txt”、“r”);
如果(!fd)
{
perror(“fopen失败”);
退出(退出失败);
}
//否则,fopen成功了
int x;
而((x=fgetc(fd))!=EOF)
{
printf(“%c”,x);
}
fclose(fd);
}
当针对任何.txt文件运行时,它将执行所需的操作
注意:我在windows或Linux上运行Linux版本18.04?仔细检查这些文件名重命名的文件是否存在?检查
errno
。使用perror
,因为它将告诉你的不仅仅是对你喊“ERROR!”。char nome\u fich[]=“text.cir.txt”代码>和fopen(“text.cir.txt”、“r”)代码>看起来像是一个混淆的机会。删除字符nome_fich[]=“text.cir.txt”
并再次测试编译/运行。发布的代码不会编译。除其他外,它缺少所需头文件的#include
语句,并且缺少任何函数,如main()