C++文件操作
核心类
| 类名 |
头文件 |
用途 |
继承自 |
默认打开模式 |
ifstream |
<fstream> |
从文件读取(输入) |
istream |
ios::in |
ofstream |
<fstream> |
向文件写入(输出) |
ostream |
ios::out(覆盖) |
fstream |
<fstream> |
读写文件 |
iostream |
需显式指定模式 |
读写对比:
| 步骤 |
文本模式 |
二进制模式 |
| 打开 |
ofstream out("file.txt"); |
ofstream out("file.bin", ios::binary); |
| 写入 |
out << "Hello" << endl; |
out.write(reinterpret_cast<char*>(&x), sizeof(x)); |
| 读取 |
in >> word; 或 getline(in, s); |
in.read(reinterpret_cast<char*>(&x), sizeof(x)); |
| 关闭 |
out.close(); |
in.close(); |
写入文件
1、文本写入
cpp
复制代码
int main()
{
system("chcp 65001");
// 写入文件
ofstream file("d:/log/123456.txt", std::ios::out | std::ios::trunc);
if (!file.is_open()) {
std::cerr << "无法打开文件 " << std::endl;
return -1;
}
// 写入UTF-8 BOM
//const unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
//file.write(reinterpret_cast<const char*>(bom), sizeof(bom));
//英文字符
std::u8string str1 = u8"hello world!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
// u8string中文
str1 = u8"测试中文内容string!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
// u8string表情符
str1 = u8"测试表情字符🌍😀 😃 😄 😁 😆!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
file.close();
return 0;
}
2、二进制写入
cpp
复制代码
int main()
{
system("chcp 65001");
// 写入文件
ofstream file("d:/log/123456.txt", std::ios::binary | std::ios::trunc);
if (!file.is_open()) {
std::cerr << "无法打开文件 " << std::endl;
return -1;
}
// 写入UTF-8 BOM
//const unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
//file.write(reinterpret_cast<const char*>(bom), sizeof(bom));
//英文字符
std::u8string str1 = u8"hello world!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
// u8string中文
str1 = u8"测试中文内容string!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
// u8string表情符
str1 = u8"测试表情字符🌍😀 😃 😄 😁 😆!";
file.write(reinterpret_cast<const char*>(str1.data()), str1.size());
file << std::endl;
file.close();
return 0;
}
读取文件
1、文本读取
cpp
复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
system("chcp 65001");
ifstream file("d:/log/123456.txt", std::ios::in);
if (!file.is_open()) {
std::cerr << "无法打开文件 " << std::endl;
return -1;
}
std::string line;
long lineCount = 0;
std::cout << getStringFromU8string(u8"=== 文本模式读取 ===") << std::endl;
while (std::getline(file, line)) {
lineCount++;
std::cout << getStringFromU8string(u8"第") << lineCount << getStringFromU8string(u8"行: ") << line << std::endl;
}
file.close();
return 0;
}
2、二进制读取
cpp
复制代码
int main()
{
system("chcp 65001");
ifstream file("d:/log/123456.txt", std::ios::binary);
if (!file.is_open()) {
std::cerr << "无法打开文件 " << std::endl;
return -1;
}
std::string line;
long lineCount = 0;
std::cout << getStringFromU8string(u8"=== 二进制模式读取 ===") << std::endl;
while (std::getline(file, line)) {
lineCount++;
std::cout << getStringFromU8string(u8"第") << lineCount << getStringFromU8string(u8"行: ") << line << endl;
}
file.close();
return 0;
}
打开模式
可通过 | 组合多个标志
| 模式 |
说明 |
适用流类型 |
std::ios::in |
以读方式打开 |
ifstream, fstream |
std::ios::out |
以写方式打开 |
ofstream, fstream |
std::ios::app |
追加写入(自动定位到文件末尾) |
ofstream, fstream |
std::ios::ate |
打开时将文件指针置于末尾(仍可写任意位置) |
所有 |
std::ios::trunc |
若文件存在则清空内容 |
ofstream(默认) |
std::ios::binary |
以二进制模式打开(非文本) |
所有 |
std::ios::_Nocreate |
指定文件不存在,则打开操作失败 |
可用exists判断 |
std::ios::_Noreplace |
指定文件已存在,则创建操作失败 |
可用exists判断 |
cpp
复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
using namespace std;
int main()
{
system("chcp 65001");
// 写入文件
ofstream ofile("d:/log/123456.txt", std::ios::binary | std::ios::out | std::ios::trunc);
//ofstream ofile("d:/log/123456.txt", std::ios::binary | std::ios::out | std::ios::app);
//ofstream ofile("d:/log/123456.txt", std::ios::binary | std::ios::out | std::ios::ate);
ofile.close();
// _Nocreate文件不存在,则打开操作失败(不会自动创建)
std::cout << getStringFromU8string(u8"------------写入文件std::ios::_Nocreate使用------------") << endl;
ofstream ofile1("d:/log/654321.txt", std::ios::binary | std::ios::_Nocreate);
std::cout << "is_open() = " << boolalpha << ofile1.is_open() << endl;
std::cout << "exists() = " << boolalpha << std::filesystem::exists("d:/log/654321.txt") << endl;
// _Noreplace如果文件已存在,则创建操作失败
std::cout << getStringFromU8string(u8"------------写入文件std::ios::_Noreplace使用------------") << endl;
ofstream ofile2("d:/log/123456.txt", std::ios::binary | std::ios::_Noreplace);
std::cout << "is_open() = " << boolalpha << ofile2.is_open() << endl;
std::cout << "exists() = " << boolalpha << std::filesystem::exists("d:/log/123456.txt") << endl;
// 读取文件
ifstream ifile("d:/log/123456.txt", std::ios::in);
//ifstream ifile("d:/log/123456.txt", std::ios::binary | std::ios::in);
ifile.close();
// _Nocreate文件不存在,则打开操作失败(不会自动创建)
std::cout << getStringFromU8string(u8"------------读取文件std::ios::_Nocreate使用------------") << endl;
ifstream ifile1("d:/log/654321.txt", std::ios::binary | std::ios::_Nocreate);
std::cout << "is_open() = " << boolalpha << ifile1.is_open() << endl;
std::cout << "exists() = " << boolalpha << std::filesystem::exists("d:/log/654321.txt") << endl;
// _Noreplace如果文件已存在,则创建操作失败
std::cout << getStringFromU8string(u8"------------读取文件std::ios::_Noreplace使用------------") << endl;
ifstream ifile2("d:/log/123456.txt", std::ios::binary | std::ios::_Noreplace);
std::cout << "is_open() = " << boolalpha << ifile2.is_open() << endl;
std::cout << "exists() = " << boolalpha << std::filesystem::exists("d:/log/123456.txt") << endl;
return 0;
}
常用方法
| 函数/方法 |
返回类型 |
说明 |
.open(filename, mode) |
void |
手动打开文件 |
.is_open() |
bool |
是否成功打开文件 ✅ 推荐检查 |
.close() |
void |
关闭文件(析构时自动调用) |
.good() |
bool |
流状态正常(无错误) |
.fail() |
bool |
发生逻辑错误(如格式不匹配) |
.bad() |
bool |
发生严重错误(如磁盘故障) |
.eof() |
bool |
到达文件末尾(⚠️ 不可靠作循环条件) |
.clear() |
void |
清除错误状态位 |
cpp
复制代码
int main()
{
system("chcp 65001");
// 读取文件
ifstream file("d:/log/123456.txt", std::ios::in);
std::cout << "is_open() = " << boolalpha << file.is_open() << endl;
std::cout << "good() = " << boolalpha << file.good() << endl;
std::cout << "bad() = " << boolalpha << file.bad() << endl;
std::cout << "fail() = " << boolalpha << file.fail() << endl;
std::cout << "eof() = " << boolalpha << file.eof() << endl;
file.clear();
file.close();
return 0;
}