任务要求:
在C程序打开数据文件,使用结束后关闭该文件
(1)用fopen函数以“读”的使用方式打开文件
(2)用fclose函数关闭数据文件
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
fp=fopen("d\\file","r");
if(fp==NULL)
{
printf("错误!不能打开文件,请检查");
getch();/*#include<conio.h>,功能是从键盘输入一个字符,但不在屏幕上显示,该行的作用是等待,然后用户再阅读出错提示*/
exit(1);/*返回值是非零,不正常退出*/
}
else
{
printf("能打开文件");
fclose(fp);
getch();
exit(1);
}
}