定义于头文件
template< class CharT, |
类模板 basic_ifstream
实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。
std::basic_ifstream
的典型实现仅保有一个非导出数据成员: std::basic_filebuf
亦定义二个对于常用字符类型的特化:
类型 | 定义 |
ifstream |
basic_ifstream |
wifstream |
basic_ifstream |
std::basic_ifstream::is_open
bool is_open(); |
(C++11 前) | |
bool is_open() const; |
(C++11 起) |
检查文件流是否有关联文件。
等效地调用 rdbuf()->is_open() 。
(无)
若文件流有关联文件,则为 true ,否则为 false 。
#include
#include
#include
#include
int main()
{
std::ifstream ifstream1("test1.txt", std::ios::in);
std::ifstream ifstream2("test2.txt", std::ios::in);
std::ifstream ifstream3("test3.txt", std::ios::in);
std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;
std::cout << std::endl;
return 0;
}
std::basic_ifstream::open
void open( const char *filename, |
(1) | |
void open( const std::filesystem::path::value_type *filename, |
(2) | (C++17 起) |
void open( const std::string &filename, |
(3) | (C++11 起) |
void open( const std::filesystem::path &filename, |
(4) | (C++17 起) |
将名为 filename
的文件打开并与文件流关联。
失败时调用 setstate(failbit) 。
成功时调用 clear() 。 | (C++11 起) |
1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::in). (该调用效果上的细节见 std::basic_filebuf::open )。仅若 std::filesystem::path::value_type 非 char 才提供重载 (2) 。 (C++17 起)
3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode) 。
filename | - | 要打开的文件名 | ||||||||||||||
mode | - | 指定打开模式。它是位掩码类型,定义下列常量:
|
(无)
#include
#include
#include
#include
int main()
{
std::string strFileName1 = "test1.txt";
std::ifstream ifstream1;
//1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::in).
ifstream1.open(strFileName1.c_str(), std::ios::in);
std::ifstream ifstream2;
std::string strFileName2 = "test2.txt";
//3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode) 。
ifstream2.open(strFileName2, std::ios::in);
std::ifstream ifstream3;
std::string strFileName3 = "test3.txt";
ifstream2.open(strFileName3, std::ios::in);
std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;
std::cout << std::endl;
return 0;
}
std::basic_ifstream::close
void close(); |
关闭关联文件。
等效地调用 rdbuf()->close() 。若操作期间出现错误,则调用 setstate(failbit) 。
(无)
(无)
此函数为 basic_ifstream 的析构函数在流对象离开作用域时调用,通常不直接调用。
#include
#include
#include
#include
int main()
{
std::ifstream ifstream1("test1.txt", std::ios::in);
std::ifstream ifstream2("test2.txt", std::ios::in);
std::ifstream ifstream3("test3.txt", std::ios::in);
std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;
std::cout << std::endl;
std::cout << "std::ifstream close" << std::endl;
ifstream1.close();
ifstream2.close();
ifstream3.close();
std::cout << std::endl;
std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;
std::cout << std::endl;
return 0;
}
std::swap(std::basic_ifstream)
template< class CharT, class Traits > |
为 std::basic_ifstream 特化 std::swap 算法。交换 lhs
与 rhs
的状态。等效地调用 lhs.swap(rhs) 。
lhs, rhs | - | 要交换状态的流 |
(无)
(无)
#include
#include
#include
#include
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;
//为 std::basic_ifstream 特化 std::swap 算法。
std::swap(ifstream1, ifstream2);
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;
}
输出