freopen

转自: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 specified stream just as fopen would do using the mode value specified as the second parameter.
This function is specially useful for redirecting predefined streams like stdin,stdout and stderr 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 the stream specified as third parater to the one specified in the mode 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 to fopen.
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 called myfile.txt, that after this program is executed contains:
This sentence is redirected to a file.
 

                             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言中的freopen函数用于实现重定向,将预定义的标准流文件定向到指定的文件中。具体来说,它可以将stdin(标准输入流,默认为键盘)、stdout(标准输出流,默认为屏幕)和stderr(标准错误流,默认为屏幕)重定向到指定的文件。 使用freopen函数需要包含头文件iostream和stdio.h,并使用命名空间std。函数的原型如下: ```c FILE* freopen(const char* path, const char* mode, FILE* stream); ``` 其中,path是重定向文件的路径,mode是打开文件的模式(如“r”表示读取,"w"表示写入,"a"表示追加),stream是要重定向的流文件。 举个例子,假设我们使用以下代码: ```c freopen("D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest.txt","w",stdout); freopen("D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest1.txt","r",stdin); ``` 第一行代码的作用是将stdout重定向到"D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest.txt"文件中,这样输出结果就可以通过打开freopenTest.txt文件来查看。 第二行代码的作用是将stdin重定向到"D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest1.txt"文件中,这样在使用scanf或cin输入时,会从freopenTest1.txt文件中获取输入。 以下是回答您的问题后的几个相关问题: 相关问题: 1. 你能解释一下重定向的作用和用途吗? 2. 除了freopen,c语言中还有其他方法可以实现重定向吗? 3. 重定向会对程序的执行效率有影响吗?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值