利用二进制方式存入读出vector文件
转自:https://blog.csdn.net/m0_37251750/article/details/94576619
#include<iostream> #include <fstream> using namespace std; #include <vector> struct TestInfo{ int id; double salary; string strName; }; typedef std::vector<TestInfo> vecTestInfo; void AnalyData() { vecTestInfo vecTemp; TestInfo info1; info1.id = 1; info1.salary = 100.2; info1.strName = "张散"; TestInfo info2; info2.id = 2; info2.salary = 112.5; info2.strName = "李四"; TestInfo info3; info3.id = 3; info3.salary = 314.7; info3.strName = "liming"; vecTemp.push_back(info1); vecTemp.push_back(info2); vecTemp.push_back(info3); ofstream osData("C:\\Users\\ZLY\\Desktop\\数据.dat", ios_base::out | ios_base::binary); osData.write(reinterpret_cast<char *>(vecTemp.data()), vecTemp.size()*sizeof(vecTemp.front()) ); osData.close(); vecTestInfo vecTemp2; ifstream isData("C:\\Users\\ZLY\\Desktop\\数据.dat", ios_base::in | ios_base::binary); if (isData) { isData.read(reinterpret_cast<char *>(vecTemp.data()), vecTemp.size()*sizeof(vecTemp2.front())); } else { AfxMessageBox(_T("ERROR: Cannot open file 数据.dat")); } isData.close(); }