#include<fstream> //for ifstream、ofstream template<typename OB> inline void saveObject(const char*filename, OB & object,int length) //传入要保存的对象引用 { std::ofstream osm(filename, std::ios::out | std::ios::binary); osm.write((const char *)&object, length*sizeof(OB)); osm.close(); } template<typename OB> inline void readObject(const char* filename, OB * object,int length) //传入读取之后对象存储的指针 { std::ifstream ism(filename, std::ios::in | std::ios::binary); ism.read((char*)object, length*sizeof(OB)); ism.close(); } int main() { uint16_t b[2] = { 1,2 }; saveObject("Data.bin", b,2); uint16_t c[2]; readObject("Data.bin", c,2); return 0; }
【参考】
【C++ 存储和读取二进制文件以及类的实例】_c++ 保存二进制文件_xhgen的博客-CSDN博客
标签:std,二进制,读写,object,ios,OB,c++,filename,char From: https://www.cnblogs.com/xixixing/p/17769819.html