程序代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fp = NULL;
int a = 0;
fp = fopen("data.txt","rt"); //打开文件
if(fp == NULL)
{
perror("data.txt");
exit(-1);
}
else
{
printf("succeed in opening the file!/n");
}
fclose(fp); //关闭文件
return 0;
}
运行结果:
1、文件存在
$ ./test
succeed in opening the file!
2、文件不存在
$ ./test
data.txt: No such file or directory