定义于头文件
template<
class CharT, > class basic_ifstream : public std::basic_istream<CharT, Traits> |
类模板 basic_ifstream 实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。
std::basic_ifstream 的典型实现仅保有一个非导出数据成员: std::basic_filebuf<CharT, Traits> 的实例。
亦定义二个对于常用字符类型的特化:
类型 | 定义 |
basic_ifstream<char> | |
basic_ifstream<wchar_t> |
成员函数
移动文件流
std::basic_ifstream<CharT,Traits>::operator=
basic_ifstream& operator=( basic_ifstream&& other ); |
(C++11 起) |
移动赋值文件流
将
参数
other | - | 要移动的文件流。 |
返回值
*this
调用示例
#include <fstream> #include <utility> #include <string> #include <iostream> int main() { std::cout << "ifstream1: " << std::endl; std::ifstream ifstream1("test.txt", std::ios::in); std::cout << "ifstream1 is: " << (ifstream1 ? "true" : "false") << std::endl; if (ifstream1.is_open()) { std::string strLine; while (std::getline(ifstream1, strLine)) { std::cout << strLine << std::endl; break; } } std::cout << std::endl; std::ifstream ifstream2 = std::move(ifstream1); std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl; std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl; std::cout << std::endl; std::cout << "ifstream2: " << std::endl; if (ifstream2.is_open()) { std::string strLine; while (std::getline(ifstream2, strLine)) { std::cout << strLine << std::endl; } } return 0; }
输出
交换二个文件流
std::basic_ifstream<CharT,Traits>::swap
void swap( basic_ifstream& other ); |
(C++11 起) |
交换流与
通过调用 basic_istream<CharT, Traits>::swap(other) 和 rdbuf()->swap(other.rdbuf()) 进行。
参数
other | - | 要交换状态的流 |
返回值
(无)
调用示例
#include <fstream> #include <utility> #include <string> #include <iostream> int main() { std::ifstream ifstream1("test1.txt", std::ios::in); std::cout << "ifstream1: " << std::endl; if (ifstream1.is_open()) { std::string strLine; uint8_t index = 0; while (std::getline(ifstream1, strLine)) { std::cout << strLine << std::endl; if (index > 1) { break; } index++; } } std::cout << std::endl; std::ifstream ifstream2("test2.txt", std::ios::in); std::cout << "ifstream2: " << std::endl; if (ifstream2.is_open()) { std::string strLine; uint8_t index = 0; while (std::getline(ifstream2, strLine)) { std::cout << strLine << std::endl; if (index > 1) { break; } index++; } } std::cout << std::endl; ifstream2.swap(ifstream1); std::cout << "ifstream1: " << std::endl; if (ifstream1.is_open()) { std::string strLine; while (std::getline(ifstream1, strLine)) { std::cout << strLine << std::endl; } } std::cout << std::endl; std::cout << "ifstream2: " << std::endl; if (ifstream2.is_open()) { std::string strLine; while (std::getline(ifstream2, strLine)) { std::cout << strLine << std::endl; } } return 0; }
输出
返回底层未处理的文件设备对象
std::basic_ifstream<CharT,Traits>::rdbuf
std::basic_filebuf<CharT, Traits>* rdbuf() const; |
(C++11 起) |
返回指向底层未处理文件设备对象的指针。
参数
(无)
返回值
指向底层未处理文件设备对象的指针。
调用示例
#include <fstream> #include <utility> #include <string> #include <iostream> int main() { std::ifstream ifstream1("test1.txt", std::ios::in); std::cout << "ifstream1: " << std::endl; if (ifstream1.is_open()) { while (ifstream1) { //返回指向底层未处理文件设备对象的指针。 std::cout << ifstream1.rdbuf() << std::endl; } } std::cout << std::endl; return 0; }