fopen():
函数原型:
FILE *fopen(const char *filename, const char *mode);
参数:
filename-- C 字符串,要打开的文件名称。
mode-- C 字符串,文件访问模式,如:r,w。
返回值:
文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL。
示例:
int main(void)
{
FILE *fp = NULL;
fp = fopen(F_PATH, "r");
if (NULL == fp)
{
return -1;
}
fclose(fp);
return 0;
}
fopen_s():
函数原型:
errno_t fopen_s( FILE** pFile, const char *filename, const char *mode );
参数:
基本同上,多了一个FILE** pFile,即文件指针的地址。
返回值:
文件顺利打开后返回0。
失败返回:(部分)
示例:
#include<stdio.h>
int main()
{
FILE* fp;
errno_t error;
error = fopen_s(&fp,F_PATH, "r");
if (error != 0 )
{
printf("打开失败");
}
fclose(fp);
return 0;
}
两者的区别:
_s后缀是因为scanf,fopen等函数在windows系统上被调用会导致不安全的结果而添加的。linux 下,直接使用fopen。