freopen
来源:互联网 发布:ido软件中文 编辑:程序博客网 时间:2024/05/22 12:46
转自:http://www.cplusplus.com/reference/clibrary/cstdio/freopen/
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
Reopen stream with different file or mode
freopen first tries to close any file already associated with the stream given as third parameter and disassociates it.
Then, whether that stream was successfuly closed or not, freopen opens the file whose name is passed in the first parameter,filename, and associates it with the specifiedstream just as fopen would do using themode value specified as the second parameter.
This function is specially useful for redirecting predefined streams like stdin,stdout andstderr to specific files (see the example below).
Parameters
- filename
- C string containing the name of the file to be opened. This parameter must follow the file specifications of the running environment and can include a path if the system supports it.
If this parameter is a null pointer, the function attemps to change the mode of thestream specified as third parater to the one specified in themode parameter, as if the file name currently associated with that stream had been used. - mode
- C string containing the file access modes. It can be:
"r"Open a file for reading. The file must exist."w"Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."a"Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist."r+"Open a file for update both reading and writing. The file must exist."w+"Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file."a+"Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek,rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.An additional b is used to specify the file is to be reopened in binary mode. For more details on these modes, refer tofopen. - stream
- pointer to a FILE object that identifies the stream to be reopened.
Return Value
If the file was successfully reopened, the function returns a pointer to an object identifying the stream. Otherwise, a null pointer is returned.
Example
/* freopen example: redirecting stdout */#include <stdio.h>int main (){ freopen ("myfile.txt","w",stdout); printf ("This sentence is redirected to a file."); fclose (stdout); return 0;}
This sample code redirects the output that would normally go to the standard output to a file calledmyfile.txt, that after this program is executed contains:
This sentence is redirected to a file.
- freopen
- freopen
- freopen
- freopen()
- freopen
- freopen()
- freopen
- freopen
- freopen
- freopen
- freopen
- freopen
- freopen
- freopen
- freopen
- freopen ()
- freopen
- freopen()
- struts2的autocompleter控件中文自动提示问题
- 深入分析 Linux 内核链表
- 可行性研究
- 菜鸟学习OGRE和天龙八部之九: AXP档案的直接载入搞定
- HttpContext.Current.Session为null的问题
- freopen
- 第6章 打印函数
- MindManager 参数不正确 解决方法
- 群里碰到一个面试题,简单明了,有助于理解2维数组和指针
- 【.Net MF网络开发板研究-06】以太网转串口
- Quit smoking
- C# String.Format格式说明
- MyEclipse中配置Struts2配置文件XML自动提示
- 菜鸟学习OGRE和天龙八部之十: frame动画基本搞定,遇到点问题