1.打开流

2.mode参数

3.fopen举例
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("a.txt","r+");
if(fp==NULL){
printf("fopen error\n");
return -1;
}
return 0;
}
4.新建文件权限

5.处理错误信息

#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("a.txt","r+");
if(fp==NULL){
perror("fopen error\n");
return -1;
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main()
{
FILE *fp;
fp=fopen("a.txt","r+");
if(fp==NULL){
printf("fopen:%s\n",strerror(errno));
return -1;
}
return 0;
}
6.关闭流

本文详细介绍了C语言中用于文件操作的fopen函数,包括打开流的步骤,mode参数的使用,例如以读写模式打开文件。同时,文章强调了新建文件时的权限设置,并展示了如何优雅地处理fopen失败时的错误信息,包括使用perror和strerror结合errno来显示详细的错误描述。最后,讨论了在完成文件操作后正确关闭流的重要性。

3646

被折叠的 条评论
为什么被折叠?



