溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載

發(fā)布時(shí)間:2020-10-22 19:32:38 來源:腳本之家 閱讀:331 作者:你是天使放縱我的固執(zhí) 欄目:編程語言

本文實(shí)例為大家分享了C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載的具體代碼,供大家參考,具體內(nèi)容如下

首先請(qǐng)先確認(rèn)已經(jīng)安裝好了opencv3及以上版本。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;

存儲(chǔ)

then

int main()
{
//創(chuàng)造一些要存的數(shù)據(jù)先
 string words = "hello, my guys!";
 float n = 3.1415926;
 Mat m = Mat::eye(3, 3, CV_32F);
 //開始創(chuàng)建存儲(chǔ)器
 FileStorage save("data.yml", FileStorage::WRITE);// 你也可以使用xml格式
 save << "words" << words;
 save << "number" << n;
 save << "matrix" << m;
 save.release();
 //存儲(chǔ)完畢
 cout << "finish storing" << endl;

加載

//加載數(shù)據(jù),類似Python字典的用法,創(chuàng)建加載器
 FileStorage load("data.yml", FileStorage::READ);
 
 float nn;
 Mat mm;
 string ww;
 load["words"] >> ww;
 load["number"] >> nn;
 load["matrix"] >> mm;
 cout<< ww << endl << nn << endl << mm;
 cout << endl << "That's the end";
 load.release();
 
 return 0;
}

完整代碼

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

int main()
{
 string words = "hello, my guys!";
 float n = 3.1415926;
 Mat m = Mat::eye(3, 3, CV_32F);
 FileStorage save("data.yml", FileStorage::WRITE);
 save << "words" << words;
 save << "number" << n;
 save << "matrix" << m;
 save.release();
 cout << "finish storing" << endl;

 FileStorage load("data.yml", FileStorage::READ);

 float nn;
 Mat mm;
 string ww;
 load["words"] >> ww;
 load["number"] >> nn;
 load["matrix"] >> mm;
 cout<< ww << endl << nn << endl << mm;
 cout << endl << "That's the end";
 load.release();

 return 0;
}

演示結(jié)果

C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI