c++如何保存vector到文件
c++保存vector到文件
實(shí)現(xiàn)將vector中的數(shù)據(jù)保存到二進(jìn)制文件和從文件中讀取數(shù)據(jù)并還原vector并不難,關(guān)鍵是要把握好一些小細(xì)節(jié)背后的原理,以及確定好存儲(chǔ)的格式。
確定格式跟網(wǎng)絡(luò)通信中的通信協(xié)議非常相似,只用存取雙方都依照協(xié)議行事,才能完成完整步驟。
以下代碼提供例子,僅供參考
#include<iostream> #include<stdio.h> #include<vector> #include<string> #include<string.h> #include<fstream> using namespace std; const int vertif = 0x1234abcd; //vertif放在文件的開(kāi)頭和結(jié)尾用于簡(jiǎn)單判斷是否正確讀取數(shù)據(jù) string filePath = "D:/TEMP/DATA"; struct Unit{ int idx; char str[10]; }; //將vector保存到二進(jìn)制文件 int saveData(){ //創(chuàng)建測(cè)試數(shù)據(jù) 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); //注意若存放的是指針類(lèi)型則需要?jiǎng)討B(tài)申請(qǐng)內(nèi)存再pushback() } //將vector保存到文件,格式:4字節(jié)檢驗(yàn)碼+4字節(jié)數(shù)組長(zhǎng)度+4字節(jié)數(shù)據(jù)長(zhǎng)度+可變長(zhǎng)度的數(shù)據(jù)+4字節(jié)尾部檢驗(yàn)碼 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; } //從二進(jìn)制中讀取之前保存的數(shù)據(jù)并還原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); //需要往文件里面放長(zhǎng)度的原因 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(); }
運(yùn)行的結(jié)果:
c++讀文件,將內(nèi)容分成兩個(gè)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; //字符串分割函數(shù) vector<string> split(string str, string pattern) { string::size_type pos; vector<string> result; str += pattern;//擴(kuò)展字符串以方便操作 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; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11C++中實(shí)現(xiàn)OpenCV圖像分割與分水嶺算法
分水嶺算法是一種常用的圖像區(qū)域分割法,本文主要介紹了OpenCV圖像分割與分水嶺算法,使用C++實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-06-06C語(yǔ)言實(shí)現(xiàn)通訊管理系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊管理系統(tǒng)設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01OpenCV相機(jī)標(biāo)定的全過(guò)程記錄
這篇文章主要給大家介紹了關(guān)于OpenCV相機(jī)標(biāo)定的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03