在线文档教程
C++
输入/输出 | Input/output

std::basic_fstream::open

性病:基本[医]Fstream::打开

void open( const char *filename, ios_base::openmode mode = ios_base::in|ios_base::out (1)
void open( const std::filesystem::path::value_type *filename, ios_base::openmode mode = ios_base::in|ios_base::out (2)(since C++17)
void open( const std::string &filename, ios_base::openmode mode = ios_base::in|ios_base::out (3)(since C++11)
void open( const std::filesystem::path &filename, ios_base::openmode mode = ios_base::in|ios_base::out (4)(since C++17)

打开文件并将其与名称关联。filename文件流。

打电话setstate(failbit)在失败的时候。

Calls clear() on success.(since C++11)

1-2%29有效呼叫rdbuf()->open(filename, mode).%28见std::basic_filebuf::open有关调用%29的效果的详细信息。仅在下列情况下才提供过载%282%29std::filesystem::path::value_type不是char.%28自C++17%29

3-4%29有效调用%281-2%29open(filename.c_str(), mode)...

参数

filename-the name of the file to be opened
mode-specifies stream open mode. It is bitmask type, the following constants are defined: Constant Explanation app seek to the end of stream before each write binary open in binary mode in open for reading out open for writing trunc discard the contents of the stream when opening ate seek to the end of stream immediately after open
ConstantExplanation
appseek to the end of stream before each write
binaryopen in binary mode
inopen for reading
outopen for writing
truncdiscard the contents of the stream when opening
ateseek to the end of stream immediately after open

返回值

%280%29

二次

#include <string> #include <fstream> #include <iostream> int main() { std::string filename = "example.123"; std::fstream fs; fs.open(filename if(!fs.is_open()) { fs.clear( fs.open(filename, std::ios::out //Create file. fs.close( fs.open(filename } std::cout << std::boolalpha; std::cout << "fs.is_open() = " << fs.is_open() << '\n'; std::cout << "fs.good() = " << fs.good() << '\n'; }

二次

另见

is_openchecks if the stream has an associated file (public member function)
closecloses the associated file (public member function)
openopens a file and configures it as the associated character sequence (public member function of std::basic_filebuf)

© cppreference.com

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

http://en.cppreference.com/w/cpp/io/basic[医]Fstream/OPEN