在下面的代码中,我试图通过char读取文件char并在控制台上显示每个char:
#include <stdio.h>
#pragma warning(disable:4996)
int main(){
FILE *file;
file = fopen("input.txt", "r");
int c;
while ((c = fgetc(file)) != EOF)
{
printf("%c ",(char)c );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
有人可以帮我解决这个问题吗?
你必须检查fopen的结果.指针文件为NULL,因为打开失败.
file = fopen("input.txt", "r");
if (file == NULL)
{
//error
}
Run Code Online (Sandbox Code Playgroud)