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

std::basic_ifstream::basic_ifstream

性病:基本[医]ifstream::basic[医]如果流

basic_ifstream((1)
explicit basic_ifstream( const char* filename, std::ios_base::openmode mode = ios_base::in (2)
explicit basic_ifstream( const std::filesystem::path::value_type* filename, std::ios_base::openmode mode = ios_base::in (3)(since C++17)
explicit basic_ifstream( const std::string& filename, std::ios_base::openmode mode = ios_base::in (4)(since C++11)
explicit basic_ifstream( const std::filesystem::path& filename, std::ios_base::openmode mode = ios_base::in (5)(since C++17)
basic_ifstream( basic_ifstream&& other (6)(since C++11)
basic_ifstream( const basic_ifstream& rhs) = delete;(7)(since C++11)

构造新的文件流。

1%29默认构造函数:构造与文件无关的流:默认构造std::basic_filebuf并使用指向此默认构造的指针构造基。std::basic_filebuf会员。

2-3%29首先执行与默认构造函数相同的步骤,然后通过调用将流与文件关联。rdbuf()->open(filename, mode |std::ios_base::in)28%见std::basic_filebuf::open有关调用%29的效果的详细信息。如果open()调用返回一个空指针,设置setstate(failbit)只在下列情况下提供过载%283%29std::filesystem::path::value_type不是char.%28自C++17%29

4-5%29basic_ifstream(filename.c_str(), mode)...

6%29移动构造函数。首先,将基类从other%28不影响rdbuf()指针%29,然后移动-构造std::basic_filebuf成员,然后调用this->set_rdbuf()安装新的basic_filebuf就像rdbuf()基类中的指针。

7%29复制构造函数被删除:该类不可复制.

参数

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
other-another file stream to use as source

二次

#include <fstream> #include <utility> #include <string> int main() { std::ifstream f0; std::ifstream f1("test.bin", std::ios::binary std::string name = "example.txt"; std::ifstream f2(name std::ifstream f3(std::move(f1) }

二次

另见

openopens a file and associates it with the stream (public member function)
openopens a file and configures it as the associated character sequence (public member function of std::basic_filebuf)
set_rdbufreplaces the rdbuf without clearing its error state (protected member function)
(constructor)constructs the object (public member function of std::basic_istream)

© cppreference.com

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

http://en.cppreference.com/w/cpp/io/basic[医]如果流/BASIC[医]如果流