
- 外文名
- fopen
- 头文件
- #include
- 函数功能
- 打开一个文件
- 中文名
- fopen
- 应 用
- C语言
-
fopen
2017-08-26 13:06:36函数原型:FILE * fopen(const char * path, const char * mode); 相关函数:open, fclose, fopen_s, _wfopen 返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL,并把...函数原型:FILE * fopen(const char * path, const char * mode);返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL,并把错误代码存在 errno 中。C语言
编辑函数简介
函数原型:FILE * fopen(const char * path, const char * mode);返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回 NULL,并把错误代码存在errno中。一般而言,打开文件后会做一些文件读取或写入的动作,若打开文件失败,接下来的读写动作也无法顺利进行,所以一般在 fopen() 后作错误判断及处理。参数说明:参数 path字符串包含欲打开的文件路径及文件名,参数 mode 字符串则代表着流形态。mode 有下列几种形态字符串:字符串 说明 r 以只读方式打开文件,该文件必须存在。 r+ 以读/写方式打开文件,该文件必须存在。 rb+ 以读/写方式打开一个二进制文件,只允许读/写数据。 rt+ 以读/写方式打开一个文本文件,允许读和写。 w 打开只写文件,若文件存在则长度清为 0,即该文件内容消失,若不存在则创建该文件。 w+ 打开可读/写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。 a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留(EOF 符保留)。 a+ 以附加方式打开可读/写的文件。若文件不存在,则会建立该文件,如果文件存在,则写入的数据会被加到文件尾后,即文件原先的内容会被保留(原来的 EOF 符不保留)。 wb 以只写方式打开或新建一个二进制文件,只允许写数据。 wb+ 以读/写方式打开或建立一个二进制文件,允许读和写。 wt+ 以读/写方式打开或建立一个文本文件,允许读写。 at+ 以读/写方式打开一个文本文件,允许读或在文本末追加数据。 ab+ 以读/写方式打开一个二进制文件,允许读或在文件末追加数据。 以 x 结尾的模式为独占模式,文件已存在或者无法创建(一般是路径不正确)都会导致 fopen 失败。文件以操作系统支持的独占模式打开。上述的形态字符串都可以再加一个 b 字符,如 rb、w+b 或 ab+ 等组合,加入 b 字符用来告诉函数库以二进制模式打开文件。如果不加 b,表示默认加了 t,即 rt、wt,其中 t 表示以文本模式打开文件。由 fopen() 所建立的新文件会具有 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666) 权限,此文件权限也会参考umask值。有些 C编译系统可能不完全提供所有这些功能,有的C版本不用"r+"、"w+"、"a+",而用"rw"、"wr"、"ar"等,读者注意所用系统的规定。二进制和文本模式的区别1、在Windows系统中,文本模式下,文件以"\r\n"代表换行。若以文本模式打开文件,并用 fputs 等函数写入换行符"\n"时,函数会自动在"\n"前面加上"\r"。即实际写入文件的是"\r\n"。2、在类 Unix/Linux 系统中文本模式下,文件以"\n"代表换行。所以 Linux 系统中在文本模式和二进制模式下并无区别。打开方式总结:各种打开方式主要有三个方面的区别1、打开是否为二进制文件,用“b”标识。2、读写的方式,有以下几种:只读、只写、读写、追加只写、追加读写这几种方式。3、对文件是否必 须存在、以及存在时是清空还是追加会有不同的响应。具体判断如下图。程序示例
示例一123456789101112131415#include <stdio.h>
#define F_PATH "d:\\myfile\\file.dat"
int
main(
void
)
{
FILE
*fp = NULL;
/* 需要注意 */
fp =
fopen
(F_PATH,
"r"
);
if
(NULL == fp)
{
return
-1;
/* 要返回错误代码 */
}
fclose
(fp);
fp = NULL;
/* 需要指向空,否则会指向原打开文件地址 */
return
0;
}
示例二1234567891011121314151617181920212223242526#include <stdio.h>
#include <stdlib.h> /* 为了使用exit() */
int
main(
void
)
{
int
i = 0;
/* 用于 putchar & getc 的数据接收 */
char
*ch =
""
;
FILE
*fp = NULL;
char
fname[50];
/* 用于存放文件名 */
printf
(
"输入文件名:"
);
scanf
(
"%s"
, fname);
fp =
fopen
(fname,
"r"
);
/* 只供读取 */
if
(NULL == fp)
/* 如果失败了 */
{
printf
(
"错误!"
);
exit
(1);
/* 中止程序 */
}
while
((ch[i] =
getc
(fp)) != EOF)
{
putchar
(ch[i]);
i ++;
}
fclose
(fp);
/* 关闭文件 */
fp = NULL;
/* 需要指向空,否则会指向原打开文件地址 */
return
0;
}
注意!初学者往往会犯一个错误,即在输入文件名时不加后缀名,请注意加上!示例三123456789101112131415161718192021222324252627#include <stdio.h>
FILE
*stream, *stream2;
int
main(
void
)
{
int
numclosed;
/* Open for read (will fail if file "crt_fopen.c" does not exist) */
if
((stream =
fopen
(
"crt_fopen.c"
,
"r"
)) == NULL)
/* C4996 */
//Note: fopen is deprecated; consider using fopen_s instead
printf
(
"The file `crt_fopen.c' was not opened\n"
);
else
printf
(
"The file `crt_fopen.c' was opened\n"
);
/* Open for write */
if
((stream2 =
fopen
(
"data2"
,
"w+"
)) == NULL)
/* C4996 */
printf
(
"The file `data2' was not opened\n"
);
else
printf
(
"The file `data2' was opened\n"
);
/* Closes tream if it is not NULL */
if
(stream)
{
if
(
fclose
(stream))
{
printf
(
"The file `crt_fopen.c' was not closed\n"
);
}
}
/* All other files are closed: */
numclosed = _fcloseall();
printf
(
"Number of files closed by _fcloseall: %u\n"
, numclosed);
}
Linux 下的程序示例。在 / opt / C_lanuage / fopen_fread 新建两个文本,main.c 和 tmp.txttmp.txt:I Love You Linux----Red Hat Enterprise----梦剧场的记忆main.c 程序:1234567891011121314151617#include <stdio.h>
int
main(
void
)
{
FILE
*fp = NULL;
char
tmp[100];
fp =
fopen
(
"/opt/C_lanuage/fopen_fread/tmp.txt"
,
"r"
);
if
(NULL == fp)
{
printf
(
"File open fail!\n"
);
return
-1;
}
fread
(tmp, 1, 100, fp);
printf
(
"%s\n"
, tmp);
fclose
(fp);
fp = NULL;
return
0;
}
编译加执行[root@localhost fopen_fread]# gcc - g main.c - o main[root@localhost fopen_fread]# . / mainI Love You Linux----Red Hat Enterprise----梦剧场的记忆注意
在文件操作时,需要注意以下几点问题:1、在定义文件指针时,要将文件指针指向空;如 FILE *fp = NULL;2、文件操作完成后,需要将文件关闭,一定要注意,否则会造成文件所占用内存泄漏和在下次访问文件时出现问题。3、文件关闭后,需要将文件指针指向空,这样做会防止出现游离指针,而对整个工程造成不必要的麻烦;如:fp = NULL; -
fopen与fopen_s的区别
2016-11-03 09:17:39fopenfopen与fopen_s的区别
在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w")。而对于fopen_s来说,还得定义另外一个变量errno_t err,然后err = fopen_s(&fp,filename,"w")。返回值的话,对于fopen来说,打开文件成功的话返回文件指针(赋值给fp),打开失败则返回NULL值;对于fopen_s来说,打开文件成功返回0,失败返回非0。 在vs编程中,经常会有这样的警告:warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS. See online help for details. 是因为 fopen_s比fopen多了溢出检测,更安全一些。(在以后的文章里还有get与get_s的比较,strcpy strcpy_s的比较,他们的共同点都是用来一些不可预料的行为) #include <stdio.h> FILE *stream, *stream2; int main( void ) { int numclosed; errno_t err; // Open for read (will fail if file "crt_fopen_s.c" does not exist) if( (err = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 ) printf( "The file 'crt_fopen_s.c' was not opened\n" ); else printf( "The file 'crt_fopen_s.c' was opened\n" );
"wb+" 以二进制模式打开文件
"w+" 以文本模式打开文件
open_s的安全是在于比函数fopen多了溢出检测。另外在使用上,函数fopen的返回值是文件指针,如果返回的文件指针为NULL时,则表示打开文件失败。而函数fopen_s的返回值是相应的错误代码,通过查看错误代码代表的含义,有助于你排查问题。还有一点,fopen_s打开的文件不能共享,如果你打开的文件需要共享的话,不能使用fopen_s函数。可以考虑_fsopen,_wfsopen这两个函数。
-
fopen改写fopen_s
2020-05-29 19:37:12在使用VS2017编译C++工程时,遇到了:错误C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. 这里记录一下: ...在使用VS2017编译C++工程时,遇到了:错误C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
这里记录一下:
FILE* pFile = fopen(filename, "rb");
改为
FILE* pFile;
if(fopen_s(&pFile, filename, "rb"))
return false;
当然也可以在工程里加入预处理器设置:_CRT_SECURE_NO_WARNINGS
两者比较:
1.fopen_s的安全是在于比函数fopen多了溢出检测;
2.在使用上,函数fopen的返回值是文件指针,如果返回的文件指针为NULL时,则表示打开文件失败。而函数fopen_s的返回值是相应的错误代码,通过查看错误代码代表的含义,有助于排查问题。
3.fopen_s打开的文件不能共享,如果你打开的文件需要共享的话,不能使用fopen_s函数。可以考虑_fopen,_wfsopen这两个函数。 -
fopen 和 fopen_s
2017-08-28 15:38:31fopen 和 fopen_s fopen用法: fopen打开文件成功,返回文件指针(赋值给fp),打开失败则返回NULL值; fp = fopen(filename,"w")。 fopen_s用法: 须定义另外一个变量errno_t err来表示文件打开是否成功 ...fopen 和 fopen_s
fopen用法:
fopen打开文件成功,返回文件指针(赋值给fp),打开失败则返回NULL值;
fp = fopen(filename,"w")。
fopen_s用法:
须定义另外一个变量errno_t err来表示文件打开是否成功
fopen_s打开文件成功返回0,失败返回非0。
err = fopen_s(&fp,filename,"w")。
在vs编程中,经常会有这样的警告:
warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS. See online help for details.
是因为 fopen_s比fopen多了溢出检测,更安全一些。
#include<stdio.h> ... FILE *stream, *stream2; int main( void ) { int numclosed; errno_t err; // Open for read (will fail if file "crt_fopen_s.c" does not exist) if( (err = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 ) printf( "The file 'crt_fopen_s.c' was not opened\n" ); else printf( "The file 'crt_fopen_s.c' was opened\n" ); // Open for write if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); // Close stream if it is not NULL if( stream) { if ( fclose( stream ) ) { printf( "The file 'crt_fopen_s.c' was not closed\n" ); } } // All other files are closed: numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }
fscanf 和 fscanf_s
fscanf用法:fscanf(fp,"%d",&var) //对比scanf
fscanf_s用法:fscanf(fp,"%d",&var,sizeof(int))
区别:fscanf_s需要指定长度
fopen 和 fopen_s,fscanf 和 fscanf_s用法的比较详解
-
fopen作用
2019-09-21 23:53:22fopen 编辑 锁定 函数原型:FILE * fopen(const char * path,const char * mode); 相关函数:open,fclose,fopen_s,_wfopen 返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回... -
fopen和fopen_s的区别
2015-11-26 22:51:46fopen: 原型:FILE * fopen(const char * path,const char * mode);接收两个实参 返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno 中。 示例程序源码:... -
fopen vs fopen64
2012-12-28 20:35:41fopen64是linux特有 的,fopen64()函数和fopen()函数相同的,只是底层的文件描述符创建是带有O_LARGEFILE标志。 fopen64()函数为了加载大型文件所做的扩展。 fopen returns FILE*, stream. fopen64 ... -
fopen和fopen_s简介
2020-05-25 22:10:531、fopen函数 (1)、定义 FILE *fopen( const char *filename, const char *mode ); r 打开只读文件,该文件必须存在。r+ 打开可读写的文件,该文件必须存在。rb+ 读写打开一个二进制文件,只允许读写数据。rt+ ... -
fopen函数
2020-08-11 15:18:46#include <stdio.h> ... fp = fopen("test.txt","a+"); if(NULL == fp) { perror("fopen"); return -1; } scanf("%c",&ch); fprintf(fp,"hello world"); printf( -
fopen 与 fopen_s
2017-08-04 14:55:21FILE * fopen(const char *file, const char *mode) { return( _fsopen(file, mode, _SH_DENYNO) ); //_SH_DENYNO允许共享读写操作 } errno_t fopen_s(FILE ** pfile, const char *file, const char *mode) -
Warning: Redefining fopen64 with std::fopen
2020-12-04 17:23:52Appears there is an issue where <code>fopen64</code> is being redefined with <code>std::fopen</code>. Build logs linked below as well in case they are of interest. <pre><code>C++ /home/conda/... -
fopen 详解
2018-07-13 08:57:05函数原型:FILE * fopen(const char * path,const char * mode); 相关函数:open,fclose,fopen_s,_wfopen 返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误...