c++保存vector到文件
实现将vector中的数据保存到二进制文件和从文件中读取数据并还原vector并不难,关键是要把握好一些小细节背后的原理,以及确定好存储的格式。
确定格式跟网络通信中的通信协议非常相似,只用存取双方都依照协议行事,才能完成完整步骤。
以下代码提供例子,仅供参考
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
#include<string.h>
#include<fstream>
using namespace std;
const int vertif = 0x1234abcd; //vertif放在文件的开头和结尾用于简单判断是否正确读取数据
string filePath = "D:/TEMP/DATA";
struct Unit{
int idx;
char str[10];
};
//将vector保存到二进制文件
int saveData(){
//创建测试数据
Unit tmp;
vector<Unit>Data;
tmp.str[0] = 'a';
for(int i=0; i<10; i++){
tmp.idx = i;
tmp.str[0] ++;
Data.push_back(tmp); //注意若存放的是指针类型则需要动态申请内存再pushback()
}
//将vector保存到文件,格式:4字节检验码+4字节数组长度+4字节数据长度+可变长度的数据+4字节尾部检验码
ofstream ofile(filePath.c_str(), ios::binary);
if(ofile.is_open()==false){
cout<<"Open file fail!"<<endl;
exit(1);
}
ofile.write((char*)&vertif, sizeof(int));
int length = Data.size();
ofile.write((char*)&length, sizeof(int));
int totalSize = Data.size()*sizeof(Unit);
ofile.write((char*)&totalSize, sizeof(int));
ofile.write((char*)&Data[0], totalSize); //注意取址方式,不能用begin()
ofile.write((char*)&vertif, sizeof(int));
ofile.close();
return 0;
}
//从二进制中读取之前保存的数据并还原vector
int restore(){
ifstream ifile(filePath.c_str(), ios::binary);
int tmpVertif, length, totalSize;
ifile.read((char*)&tmpVertif, sizeof(int));
if (tmpVertif!=vertif){
cout<<"Unknow format at the begin of file...";
exit(1);
}
ifile.read((char*)&length, sizeof(int));
ifile.read((char*)&totalSize, sizeof(int));
vector<Unit>Data(length); //需要往文件里面放长度的原因
ifile.read((char*)&Data[0], totalSize);
ifile.read((char*)&tmpVertif, sizeof(int));
if (tmpVertif!=vertif){
cout<<"Unknow format at the end of file..."<<endl;
exit(1);
}
for(int i=0; i<Data.size(); i++){
cout<< Data[i].idx <<" "<<Data[i].str[0] <<endl;
}
return 0;
}
int main(){
saveData();
restore();
}
运行的结果:
c++读文件,将内容分成两个vector
//123.txt
ABS 3
Acrylic 4.8
Aluminum 70
Cortical_bone 18
Cancellous_bone 3.7
Carbon_fiber 150
Co_Cr 53.2
Concrete 17
CFRP 17.9
PEEK 3.8
Platinum 147
Silicon_carbide 450
Stainless_steel 51
Titanium 50.2
#include<iostream>
#include <fstream>
#include <sstream>
#include<string>
#include <vector>
using namespace std;
# define MAX 2000
int main()
{
ifstream in_file("123.txt");
string a;
vector<string> s;
vector<double> d;
while (getline(in_file, a)) {
istringstream record(a);
string word;
vector<string> vs;
while (record >> word)
vs.push_back(word);
string aa;
double dd;
aa = vs[0];
dd = stod(vs[1]);
s.push_back(aa);
d.push_back(dd);
}
for (int i = 0; i < s.size(); i++)
cout<<s[i] << " " <<endl;
for (int i = 0; i < s.size(); i++)
cout << d[i] << endl;
return 0;
}
#include<iostream>
#include <fstream>
#include<string>
#include <vector>
using namespace std;
//字符串分割函数
vector<string> split(string str, string pattern)
{
string::size_type pos;
vector<string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
int main()
{
ifstream in_file("123.txt");
string a;
vector<string> s;
vector<double> d;
while (getline(in_file, a)) {
vector<string> vs;
string pattern = "\t";
string::size_type pos;
a = a + pattern;
for (int i = 0; i < a.size(); i++)
{
pos = a.find(pattern, i);
if (pos < a.size())
{
std::string s = a.substr(i, pos - i);
vs.push_back(s);
i = pos + pattern.size() - 1;
}
}
string aa;
double dd;
aa = vs[0];
dd = stod(vs[1]);
s.push_back(aa);
d.push_back(dd);
}
for (int i = 0; i < s.size(); i++) cout<<s[i] << " " <<endl;
for (int i = 0; i < s.size(); i++) cout << d[i] << endl;
return 0;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。