首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::freopen

Defined in header <cstdio>

?

?

std::FILE* freopen( const char* filename, const char* mode, std::FILE* stream );

?

?

首先,尝试关闭与stream忽略任何错误。那么,如果filename不为空,则尝试打开filename使用mode好像fopen,并将该文件与stream.如果filename为空指针,则该函数尝试重新打开已与stream%28--在本例中,定义了允许更改模式的实现--%29。

参数

filename

-

file name to associate the file stream to

mode

-

null-terminated character string determining new file access mode File access mode string Meaning Explanation Action if file already exists Action if file does not exist "r" read Open a file for reading read from start failure to open "w" write Create a file for writing destroy contents create new "a" append Append to a file write to end create new "r+" read extended Open a file for read/write read from start error "w+" write extended Create a file for read/write destroy contents create new "a+" append extended Open a file for read/write write to end create new File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A'. On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator.

File access mode string

Meaning

Explanation

Action if file already exists

Action if file does not exist

"r"

read

Open a file for reading

read from start

failure to open

"w"

write

Create a file for writing

destroy contents

create new

"a"

append

Append to a file

write to end

create new

"r+"

read extended

Open a file for read/write

read from start

error

"w+"

write extended

Create a file for read/write

destroy contents

create new

"a+"

append extended

Open a file for read/write

write to end

create new

File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A'. On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator.

File access mode string

Meaning

Explanation

Action if file already exists

Action if file does not exist

"r"

read

Open a file for reading

read from start

failure to open

"w"

write

Create a file for writing

destroy contents

create new

"a"

append

Append to a file

write to end

create new

"r+"

read extended

Open a file for read/write

read from start

error

"w+"

write extended

Create a file for read/write

destroy contents

create new

"a+"

append extended

Open a file for read/write

write to end

create new

可以选择指定文件访问模式标志“b”以二进制模式打开文件。此标志对POSIX系统没有影响,但在Windows上,它禁用%27\n%27和%27\x1A%27的特殊处理。在附加文件访问模式上,数据被写入文件的末尾,而不管文件位置指示符的当前位置。

流-要修改的文件流。

返回值

stream在成功的时候,NULL在失败的时候。

注记

freopen是更改流的窄/宽方向的唯一方法,一旦由I/O操作或std::fwide...

以下代码重定向stdout文件。

二次

代码语言:javascript
复制
#include <cstdio>
 
int main()
{
    std::printf("stdout is printed to console\n");
    if(std::freopen("redir.txt", "w", stdout)) {
        std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
        std::fclose(stdout);
    }
}

二次

产出:

二次

代码语言:javascript
复制
stdout is printed to console

二次

另见

fopen

opens a file (function)

fclose

closes a file (function)

C.FREOPEN文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com