fstream是 C++ 标准库中用于文件操作的头文件,提供了文件输入输出流的功能,可以创建文件流对象并使用它们来读取和写入文件的内容。 |
#include <fstream> |
一、文件输入流(std::ifstream)
1、构造函数
- ifstream::ifstream():默认构造函数,创建一个未关联到任何文件的输入文件流对象;
- ifstream::ifstream(const char* filename):带参数的构造函数,创建一个与指定文件关联的输入文件流对象。
|
2、打开/关闭文件
- void ifstream::open(const char* filename, ios_base::openmode mode = ios_base::in):打开指定文件,并指定打开模式(默认为只读);
- void ifstream::close():关闭文件。
|
3、读取文件内容
- bool ifstream::is_open():检查文件是否成功打开;
- bool ifstream::good():检查文件是否处于正常状态(即没有错误发生);
- bool ifstream::eof():检查是否已到达文件末尾;
- void ifstream::clear():清除流的错误状态标志;
- char ifstream::get():从文件中读取一个字符;
- string ifstream::getline(string& str):从文件中读取一行数据到字符串中;
- istream& ifstream::operator>>(type& val):从文件中读取指定类型的数据。
|
4、位置和状态
- streampos ifstream::tellg():获取当前读取位置;
- void ifstream::seekg(streampos pos):设置读取位置到指定位置;
- void ifstream::seekg(streamoff off, ios_base::seekdir way):根据指定偏移量和方式设置读取位置。
|
5、示例代码
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inFile("example.txt"); // 创建一个名为example.txt 的输入文件流对象
if (inFile.is_open()) { // 检查文件是否成功打开
std::string line;
while (std::getline(inFile, line)) { // 逐行读取文件内容
std::cout << line << std::endl; // 输出每行内容到控制台
}
inFile.close(); // 关闭文件流
std::cout << "File successfully read." << std::endl;
} else {
std::cout << "Error opening file!" << std::endl;
return 1;
}
return 0;
}
二、文件输出流(std::ofstream)
1、构造函数
- ofstream(): 默认构造函数,创建一个未关联到任何文件的输出文件流;
- explicit ofstream(const char* filename, ios_base::openmode mode = ios_base::out): 构造函数,打开一个文件以供写入,参数 filename 是要打开的文件名,mode 是打开文件的模式,默认为输出模式。
|
2、打开/关闭文件
- void open(const char* filename, ios_base::openmode mode = ios_base::out): 打开一个文件以供写入。参数与构造函数相同;
- void close(): 关闭文件流,释放关联的资源。
|
3、写入文件内容
- bool is_open() const: 检查文件流是否已经成功打开一个文件;
- bool good() const: 检查流是否处于良好状态,即没有错误发生;
- void clear(): 清除流的错误状态;
- bool fail() const: 检查流是否发生了错误,但错误可以被清除;
- bool bad() const: 检查流是否发生了不可恢复的错误;
- ofstream& operator<<(T val): 将数据 val 写入文件流中,支持各种数据类型的写入操作;
- ostream& write(const char* s, streamsize n): 将字符数组 s 中的 n 个字节写入文件流中。
|
4、位置和状态
- pos_type tellp(): 返回当前写入位置的文件指针位置。
- ostream& seekp(pos_type pos): 设置文件指针位置,用于移动写入位置。
- ostream& seekp(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置。
|
5、示例代码
#include <iostream>
#include <fstream>
int main() {
// 创建 ofstream 对象并以追加模式打开文件
std::ofstream outputFile("example.txt", std::ios::app);
// 检查文件是否成功打开
if (outputFile.is_open()) {
// 写入数据到文件
outputFile << "This is additional text appended to the file.\n";
outputFile << "Appending some numbers: " << 456 << " " << 2.718 << "\n";
// 关闭文件
outputFile.close();
std::cout << "File append operation successful.\n";
} else {
// 文件打开失败
std::cerr << "Unable to open file.\n";
}
return 0;
}
三、文件输入/输出流(std::fstream)
1、构造函数
- fstream(): 默认构造函数,创建一个未关联到任何文件的文件流;
- explicit fstream(const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out): 构造函数,打开一个文件以供读写,参数 filename 是要打开的文件名,mode 是打开文件的模式,默认为输入输出模式。
|
2、打开/关闭文件
- void open(const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out): 打开一个文件以供读写,参数与构造函数相同;
- void close(): 关闭文件流,释放关联的资源。
|
3、读写文件内容
- operator>>: 从文件流中提取数据;
- operator<<: 向文件流中插入数据;
- istream& getline(char* s, streamsize n): 从文件流中读取一行数据;
- ostream& write(const char* s, streamsize n): 将字符数组 s 中的 n 个字节写入文件流中。
|
4、位置和状态
- pos_type tellg(): 返回当前读取位置的文件指针位置;
- istream& seekg(pos_type pos): 设置文件指针位置,用于移动读取位置;
- istream& seekg(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置;
- pos_type tellp(): 返回当前写入位置的文件指针位置;
- ostream& seekp(pos_type pos): 设置文件指针位置,用于移动写入位置;
- ostream& seekp(off_type off, ios_base::seekdir way): 以指定方式移动文件指针位置。
|
5、示例代码
#include <iostream>
#include <fstream>
#include <string>
int main() {
// 创建 fstream 对象并打开文件
std::fstream file("example.txt", std::ios::out | std::ios::in);
// 检查文件是否成功打开
if (file.is_open()) {
// 写入数据到文件
file << "This is a sample text.\n";
file << "Writing some numbers: " << 123 << " " << 3.14 << "\n";
// 将文件指针移动到文件开头
file.seekg(0, std::ios::beg);
// 读取文件内容并打印到控制台
std::string line;
while (std::getline(file, line)) { // 确保使用std::getline函数
std::cout << "Line from file: " << line << "\n";
}
// 关闭文件
file.close();
std::cout << "File read/write operation successful.\n";
} else {
// 文件打开失败
std::cerr << "Unable to open file.\n";
}
return 0;
}