fopen()用于打开,而fclose()用于关闭C中的文件
#include
#include // For exit()
int main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;
}
输出:
Enter the filename to open
a.txt
/*Contents of a.txt*/
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。