C++ 文件和流
在 C++ 中,文件和流是进行文件操作的基本机制。通过文件流(File Streams),可以对文件进行读取、写入以及其他各种操作。C++ 标准库提供了 fstream 类库来处理文件的输入输出。
1. 文件流的基本概念
文件流是由 C++ 的 fstream 类提供的一种机制,用于对文件进行操作。文件流分为三类:
- 输入文件流(ifstream):用于从文件读取数据。
- 输出文件流(ofstream):用于向文件写入数据。
- 文件流(fstream):同时支持读写文件的操作。
2. 文件流的使用
2.1 打开文件
在 C++ 中,文件流对象需要通过 open() 方法打开文件。文件打开后,就可以进行相应的读写操作。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 创建一个 ofstream 对象(输出文件流)
ofstream outFile;
// 打开文件,如果文件不存在则创建文件
outFile.open("example.txt");
// 检查文件是否成功打开
if (!outFile) {
cerr << "Error opening file for writing!" << endl;
return 1;
}
// 向文件写入数据
outFile << "Hello, C++ file operations!" << endl;
// 关闭文件
outFile.close();
return 0;
}
说明:
open():打开指定的文件,ofstream会创建文件,如果文件已经存在,会清空文件内容。close():操作完成后关闭文件。
2.2 从文件读取数据
使用 ifstream 类来从文件中读取数据。读取的方式通常有两种:
- 逐行读取
- 按单个字符或格式读取
2.2.1 逐行读取
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// 创建 ifstream 对象(输入文件流)
ifstream inFile("example.txt");
// 检查文件是否成功打开
if (!inFile) {
cerr << "Error opening file for reading!" << endl;
return 1;
}
string line;
// 逐行读取文件
while (getline(inFile, line)) {
cout << line << endl;
}
// 关闭文件
inFile.close();
return 0;
}
2.2.2 按字符读取
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 创建 ifstream 对象
ifstream inFile("example.txt");
// 检查文件是否成功打开
if (!inFile) {
cerr << "Error opening file for reading!" << endl;
return 1;
}
char ch;
// 逐字符读取文件
while (inFile.get(ch)) {
cout << ch;
}
// 关闭文件
inFile.close();
return 0;
}
2.3 写入数据到文件
与读取文件类似,我们可以通过 ofstream 将数据写入文件。写入数据时可以使用多种方式:
- 写入字符串
- 写入数字
- 写入格式化数据
2.3.1 写入字符串和数字
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 创建 ofstream 对象
ofstream outFile("example_output.txt");
// 检查文件是否成功打开
if (!outFile) {
cerr << "Error opening file for writing!" << endl;
return 1;
}
// 写入字符串和数字
outFile << "Hello, C++ file handling!" << endl;
outFile << "The number is: " << 42 << endl;
// 关闭文件
outFile.close();
return 0;
}
2.4 读写文件(fstream)
如果要同时读写文件,可以使用 fstream 类。这个类继承自 ifstream 和 ofstream,支持两者的操作。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 创建 fstream 对象(既可以读取也可以写入)
fstream file;
// 打开文件进行读写
file.open("example.txt", ios::in | ios::out | ios::app);
// 检查文件是否成功打开
if (!file) {
cerr << "Error opening file for reading/writing!" << endl;
return 1;
}
// 读取文件内容
string content;
while (getline(file, content)) {
cout << content << endl;
}
// 在文件末尾写入内容
file << "\nAppending new content to the file.";
// 关闭文件
file.close();
return 0;
}
2.5 文件流模式
fstream 和 ofstream 支持不同的文件打开模式。常见的模式包括:
- ios::in:打开文件进行读取。
- ios::out:打开文件进行写入。
- ios::app:打开文件并将数据追加到文件末尾。
- ios::ate:打开文件,并将文件指针移动到文件的末尾。
- ios::trunc:如果文件存在,则文件内容会被清空(默认行为)。
ofstream outFile("example.txt", ios::out | ios::trunc);
3. 文件操作的错误处理
在进行文件操作时,务必检查文件是否成功打开。可以使用 fail() 方法检查文件是否打开成功,或者检查文件流的状态。
ifstream inFile("example.txt");
if (inFile.fail()) {
cerr << "Error opening file!" << endl;
return 1;
}
4. 文件指针操作
文件流对象维护着一个内部的文件指针,可以使用一些方法来操作文件指针的位置:
- seekg():设置输入文件流的指针位置(读取)。
- seekp():设置输出文件流的指针位置(写入)。
- tellg():获取当前输入流的指针位置。
- tellp():获取当前输出流的指针位置。
fstream file("example.txt", ios::in | ios::out);
// 将文件指针移到文件开头
file.seekg(0, ios::beg);
// 获取文件当前指针位置
cout << "Current position: " << file.tellg() << endl;
5. 总结
在 C++ 中,文件操作是通过文件流(fstream, ifstream, ofstream)来实现的。通过这些类,你可以读取、写入和修改文件中的内容。了解并熟练掌握文件流操作,可以使得你能够更高效地处理数据存储与读取任务。
更多详细内容请关注其他相关的文章。