学习C++:读写二进制文件
上一篇博客https://blog.csdn.net/wjinjie/article/details/104303770中,已经总结了处理普通文本文件的读写流程。 处理二进制文件的流程与上一篇介绍的流程差别不大,不同的是在打开文件时使用ios_base::binary标志。通常使用ofstream::write和ifstream::read来读写二进制文件。 首先补充函数open()打开文件流的各种模式: ios_base::binary 创建二进制文件 ios_base::in 以只读方式打开文件 ios_base::out 以只写方式打开文件 ios_base::trunc 重新创建一个文件(即时指定的文件已经存在) ios_base::app 附加到现有文件末尾,而不是覆盖它 ios_base::ate 切换到文件末尾,但可在文件的任何地方写入数据 以下程序将一个结构写入二进制文件并使用该文件的内容创建一个结构: # include <fstream> # include <iomanip> # include <string> # include <iostream> using namespace std ; struct Human { Human ( ) { } ; Human ( const char * inName , int inAge , const