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

std::fopen

Defined in header <cstdio>

?

?

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

?

?

打开由filename并返回与该文件关联的文件流。mode用于确定文件访问模式。

参数

filename

-

file name to associate the file stream to

mode

-

null-terminated character string determining 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的特殊处理。在附加文件访问模式上,数据被写入文件的末尾,而不管文件位置指示符的当前位置。

返回值

如果成功,则返回指向控制打开的文件流的对象的指针,清除eof和Error位。除非文件名引用交互设备,否则流将被完全缓冲。

如果出现错误,则返回一个空指针。POSIX要求errno在这种情况下。

注记

格式filename是实现定义的,不一定引用文件%28例如。它可能是通过文件系统API%29访问的控制台或其他设备。在支持他们的平台上,filename可以包括绝对或相对的文件系统路径。

有关可移植目录和文件命名,请参见C++文件系统库或加油。文件系统...

二次

代码语言:javascript
复制
#include <cstdio>
#include <cstdlib>
 
int main()
{
    FILE* fp = std::fopen("test.txt", "r");
    if(!fp) {
        std::perror("File opening failed");
        return EXIT_FAILURE;
    }
 
    int c; // note: int, not char, required to handle EOF
    while ((c = std::fgetc(fp)) != EOF) { // standard C I/O file reading loop
       std::putchar(c);
    }
 
    if (std::ferror(fp))
        std::puts("I/O error when reading");
    else if (std::feof(fp))
        std::puts("End of file reached successfully");
 
    std::fclose(fp);
}

二次

另见

fclose

closes a file (function)

fflush

synchronizes an output stream with the actual file (function)

freopen

open an existing stream with a different name (function)

c fopen文件

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com