2026/6/20 2:25:44
网站建设
项目流程
网站名称可以更换吗,在线制作图片及图片处理,店铺网站域名怎么做,网站 前端 后端一、核心头文件与基础类1. 必备头文件fstream#xff1a;所有文件流操作的核心头文件#xff0c;包含 ifstream/ofstream/fstream 三类核心文件流类#xff1b;iostream#xff1a;提供流基类和基础输入输出能力#xff08;如 cin/cout#xff09;#…一、核心头文件与基础类1. 必备头文件fstream所有文件流操作的核心头文件包含ifstream/ofstream/fstream三类核心文件流类iostream提供流基类和基础输入输出能力如cin/cout文件流类均继承自其基类string用于处理文件路径字符串、读写的文本内容cstdio补充文件重命名、删除等高级操作兼容 C 标准库。2. 核心文件流类std::ifstream只读文件流专门用于从文件读取数据文本 / 二进制均可默认以ios::in模式打开std::ofstream只写文件流专门用于向文件写入数据文本 / 二进制均可默认以ios::out | ios::trunc模式打开std::fstream读写文件流支持同时读取和写入文件默认以ios::in | ios::out模式打开。二、文件打开模式文件打开模式用于指定文件的操作规则可通过|组合多个模式所有模式均属于std::ios_base::openmode类型ios::in功能以只读方式打开文件文件不存在则打开失败适用场景读取配置文件、文本内容等只读操作是ifstream的默认模式常用组合ios::in | ios::binary二进制只读模式。ios::out功能以只写方式打开文件文件不存在则自动创建文件存在则清空原有内容适用场景写入新文件、覆盖原有文件内容是ofstream的默认模式常用组合ios::out | ios::app追加写模式。ios::app功能追加写入模式所有写入内容都会添加到文件末尾不会清空原有内容适用场景日志文件写入、累计数据记录等需要保留历史内容的场景。ios::binary功能二进制模式打开文件不处理换行符Windows 下\r\n/Linux 下\n等特殊字符直接读写原始字节适用场景图片、视频、自定义结构体等非文本数据的读写。ios::trunc功能打开文件时清空原有内容ios::out模式默认包含此属性注意与ios::app冲突同时指定时app优先级更高。ios::ate功能打开文件后文件指针直接定位到文件末尾可通过指针操作移动到其他位置典型用法快速获取文件总大小定位到末尾后调用tellg()。三、核心函数说明签名 功能 示例1. 文件打开与关闭1构造函数直接打开签名以ifstream为例std::ifstream::ifstream(const std::string path, std::ios_base::openmode mode ios::in)功能创建流对象的同时打开指定路径的文件一步完成对象初始化和文件打开// 只读打开文本配置文件默认ios::in模式 std::ifstream config_in(config.txt); // 追加模式打开日志文件ios::out ios::app std::ofstream log_out(app.log, std::ios::out | std::ios::app);2open () 显式打开签名void std::ifstream::open(const std::string path, std::ios_base::openmode mode ios::in)功能为已创建的空流对象绑定并打开指定文件适用于需要延迟打开文件的场景std::fstream data_fs; // 读写二进制模式打开数据文件 data_fs.open(data.bin, std::ios::in | std::ios::out | std::ios::binary);3is_open () 检查打开状态签名bool std::ifstream::is_open() const功能判断文件是否成功打开返回true表示成功false表示失败路径错误、权限不足等std::ifstream in(test.txt); if (!in.is_open()) { std::cerr 文件打开失败test.txt std::endl; return 1; // 异常退出 }4close () 关闭文件签名void std::ifstream::close()功能关闭已打开的文件释放系统文件句柄资源std::ofstream out(log.txt); // 写入内容后显式关闭 out.close();2. 文本文件读写1getline () 逐行读取签名std::istream std::getline(std::istream is, std::string str)功能从输入流中读取一行文本直到换行符不包含换行符到字符串变量中std::ifstream in(config.txt); std::string line; // 逐行读取直到文件末尾 while (std::getline(in, line)) { std::cout 读取行 line std::endl; }2 运算符 按空白符读取功能从文件流中读取数据单词、数字等自动跳过空格、换行、制表符等空白符std::ifstream in(num.txt); // 文件内容10 20 30 40 int num; // 逐个读取整数 while (in num) { std::cout 读取数字 num std::endl; }3get () 单字符读取签名std::istream std::istream::get(char c)功能读取单个字符包括空白符、换行符到字符变量中保留文件原始格式std::ifstream in(test.txt); char ch; // 逐字符读取并输出 while (in.get(ch)) { std::cout ch; }4 运算符 格式化写入功能与std::cout用法完全一致向文件流写入格式化数据字符串、数字、浮点数等std::ofstream out(log.txt); out 程序启动时间 2026-01-26 std::endl; out 用户ID 1001 | 分数 95.5 std::endl;5put () 单字符写入签名std::ostream std::ostream::put(char c)功能向文件流写入单个字符支持链式调用std::ofstream out(char.txt); // 链式写入字符最终文件内容Hello\n out.put(H).put(e).put(l).put(l).put(o).put(\n);3. 二进制文件读写1read () 二进制读取签名std::istream std::istream::read(char* buffer, std::streamsize count)功能从文件流中读取指定字节数的二进制数据到缓冲区// 读取4字节整数二进制模式 std::ifstream in(data.bin, std::ios::in | std::ios::binary); int num; // 强制转换为char*读取int大小的字节数 in.read(reinterpret_castchar*(num), sizeof(num)); std::cout 读取的整数 num std::endl;2write () 二进制写入签名std::ostream std::ostream::write(const char* buffer, std::streamsize count)功能将缓冲区中的指定字节数的二进制数据写入文件流// 写入自定义结构体二进制模式 struct Student { int id; char name[20]; float score; }; std::ofstream out(stu.bin, std::ios::out | std::ios::binary); Student s {1001, 张三, 95.5f}; out.write(reinterpret_castconst char*(s), sizeof(s));4. 文件指针与随机访问文件流通过「读指针get」和「写指针put」实现随机访问读指针操作函数以g结尾写指针以p结尾1seekg () 移动读指针签名 1绝对位置std::istream std::istream::seekg(std::streampos pos)签名 2相对位置std::istream std::istream::seekg(std::streamoff off, std::ios_base::seekdir dir)功能移动读指针到指定位置dir可选基准位置ios::beg从文件开头偏移ios::cur从当前指针位置偏移ios::end从文件末尾偏移std::ifstream in(test.txt); in.seekg(0, std::ios::end); // 指针移到文件末尾用于获取文件大小 in.seekg(10, std::ios::beg); // 指针移到文件开头后第10字节2tellg () 获取读指针位置签名std::streampos std::istream::tellg() const功能返回当前读指针的位置字节数从文件开头算起std::ifstream in(test.txt); in.seekg(0, std::ios::end); std::streampos file_size in.tellg(); // 获取文件总字节数 std::cout 文件大小 file_size 字节 std::endl;3seekp () 移动写指针功能与签名同seekg()区别是操作写指针适用于写文件或读写文件场景std::fstream fs(data.bin, std::ios::in | std::ios::out | std::ios::binary); fs.seekp(5); // 写指针移到第5字节位置绝对位置4tellp () 获取写指针位置功能与签名同tellg()区别是返回写指针的当前位置std::ofstream out(test.txt); out Hello; std::streampos pos out.tellp(); // 返回5Hello共5字节5. 错误处理函数文件操作需检查流状态避免操作失败导致程序异常1eof()签名bool std::ios::eof() const功能判断是否到达文件末尾EOF返回true表示正常读取到末尾std::ifstream in(test.txt); std::string line; while (std::getline(in, line)) {} if (in.eof()) { std::cout 文件读取完成正常到达末尾 std::endl; }2fail()签名bool std::ios::fail() const功能判断是否发生非致命错误如格式错误读取整数时读到字母if (in.fail()) { std::cerr 文件读取格式错误非致命 std::endl; }3bad()签名bool std::ios::bad() const功能判断是否发生致命错误如文件损坏、磁盘故障、流被破坏if (in.bad()) { std::cerr 文件读取致命错误如文件损坏 std::endl; }4clear()签名void std::ios::clear(std::ios_base::iostate state std::ios_base::goodbit)功能清除流的错误状态如eof()为true后需调用clear()才能继续操作in.clear(); // 清除所有错误状态 in.seekg(0); // 重置读指针到文件开头6. 高级文件操作1文件重命名函数std::rename来自cstdio功能修改文件名成功返回 0失败返回非 0#include cstdio if (std::rename(old.txt, new.txt) ! 0) { std::cerr 文件重命名失败 std::endl; }2文件删除函数std::remove来自cstdio功能删除指定文件成功返回 0失败返回非 0#include cstdio if (std::remove(tmp.txt) ! 0) { std::cerr 文件删除失败 std::endl; }3获取文件大小实现方式结合seekg()和tellg()std::ifstream in(test.bin, std::ios::binary); in.seekg(0, std::ios::end); // 指针移到末尾 int size in.tellg(); // 获取字节数 in.seekg(0, std::ios::beg); // 移回开头方便后续操作 std::cout 文件大小 size 字节 std::endl;四、常用场景完整示例场景 1逐行读取文本配置文件#include fstream #include iostream #include string using namespace std; int main() { // 打开配置文件 ifstream config_in(config.conf); if (!config_in.is_open()) { cerr 配置文件打开失败config.conf endl; return 1; } // 逐行读取并输出 string line; cout 配置文件内容 endl; while (getline(config_in, line)) { cout line endl; } // 检查读取状态 if (config_in.eof()) { cout \n配置文件读取完成 endl; } config_in.close(); return 0; }场景 2追加写入日志文件#include fstream #include iostream #include string using namespace std; int main() { // 追加模式打开日志文件 ofstream log_out(app.log, ios::out | ios::app); if (!log_out) { cerr 日志文件打开失败 endl; return 1; } // 写入日志内容 log_out 【INFO】程序启动成功 | 时间2026-01-26 15:30:00 endl; log_out 【DEBUG】用户ID1001 | 操作登录 endl; log_out.close(); cout 日志写入完成 endl; return 0; }场景 3二进制读写结构体数据#include fstream #include iostream using namespace std; // 自定义数据结构体 struct UserData { int id; char username[16]; double balance; }; int main() { // 1. 写入二进制数据 ofstream out(user_data.bin, ios::out | ios::binary); if (!out) { cerr 写入文件打开失败 endl; return 1; } UserData user {1002, 李四, 5000.50}; out.write(reinterpret_castconst char*(user), sizeof(user)); out.close(); // 2. 读取二进制数据 ifstream in(user_data.bin, ios::in | ios::binary); if (!in) { cerr 读取文件打开失败 endl; return 1; } UserData read_user; in.read(reinterpret_castchar*(read_user), sizeof(read_user)); in.close(); // 输出读取结果 cout 用户ID read_user.id endl; cout 用户名 read_user.username endl; cout 余额 read_user.balance endl; return 0; }场景 4批量打开并读取多个文件#include fstream #include iostream #include vector #include string using namespace std; int main() { // 待打开的文件列表 vectorstring file_list {file1.txt, file2.txt, file3.txt}; // 存储多个读流对象使用移动语义ifstream不可拷贝 vectorifstream file_ins; // 批量打开文件 for (const string path : file_list) { ifstream in(path); if (!in) { cerr 跳过失败文件 path endl; continue; } file_ins.push_back(move(in)); // 移动语义存入容器 } // 批量读取每个文件 for (int i 0; i file_ins.size(); i) { cout \n 读取文件 file_list[i] endl; string line; while (getline(file_ins[i], line)) { cout line endl; } file_ins[i].close(); } return 0; }五、关键注意事项打开文件后必须检查is_open()路径错误、权限不足、文件不存在等都会导致打开失败未检查会引发后续操作崩溃流对象不可拷贝ifstream/ofstream/fstream均禁用拷贝构造和赋值批量管理时需用std::move实现移动二进制模式必加ios::binary读写图片、结构体等非文本数据时不加此模式会导致换行符被篡改数据错误错误状态需清除调用eof()/fail()为true后流会进入错误状态需用clear()清除后才能继续操作及时关闭文件虽然流对象析构时会自动关闭文件但显式调用close()能及时释放文件句柄避免资源泄露路径分隔符跨平台代码优先使用/Windows 和 Linux 均支持避免\Windows 专属需转义为\\。总结文件操作核心是「流对象 打开模式 读写方法」选对类ifstream/ofstream/fstream和模式是基础文本文件用getline()/读写二进制文件用read()/write()ios::binary随机访问依赖seekg()/tellg()/seekp()/tellp()错误处理需结合eof()/fail()/clear()批量操作多个文件时用vector存储流对象并配合移动语义避免拷贝错误。