运行如下测试代码: 使用GHex打开生成的文件,内容完全相同。 故使用类来保存数据并不会产生额外的内容。
C++二进制文件保存数据 类与结构体的区别
// OS: Ubuntu 19.10 // gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) #include <iostream> #include <fstream> #include <cstdlib> #include <string> using std::string; using std::ofstream; using std::cout; using std::cerr; using std::endl; using std::ios; class Test { private: int value; int data; public: Test() { value = 0; data = 0; } Test(int num1, int num2) : value(num1), data(num2) {} void show() { cout << value << ' ' << data << endl; } int getValue() const { return value; } int getData() const { return data; } void setValue(int num) { value = num; } void setData(int num) { data = num; } }; struct TestSt { int value; int data; }; int main(int argc, char const *argv[]) { const string file_path_class = "/home/aimerneige/temp/class.aimt"; const string file_path_struct = "/home/aimerneige/temp/struct.aimt"; Test t(15, 255); TestSt ts = {15, 255}; ofstream outfile_class(file_path_class, ios::binary); if (!outfile_class) { cerr << "Can't open the file outfile_class" << endl; exit(1); } else { cout << "Open outfile_class success!" << endl; } outfile_class.write((char *) &t, sizeof(t)); outfile_class.close(); ofstream outfile_struct(file_path_struct, ios::binary); if (!outfile_struct) { cerr << "Can't open the file outfile_struct" << endl; exit(1); } else { cout << "Open outfile_struct success!" << endl; } outfile_struct.write((char *) &ts, sizeof(ts)); outfile_struct.close(); return 0; } // The file in disk are same.
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算