c++数组的二进制文件读写
#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; }
【参考】