我刚开始学习C而我正试图获得这种类型的代码:
Enter text file: cat.txt
然后当它运行时它应显示所有文本但如果它不是有效的文本文件则应该抛出错误
答案 0 :(得分:-1)
使用类似的东西
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}