本文實例為大家分享了C++實現數據文件存儲與加載的具體代碼,供大家參考,具體內容如下
首先請先確認已經安裝好了opencv3及以上版本。
#include <opencv2/opencv.hpp>#include <iostream>#include <string>using namespace cv;using namespace std;
存儲
then
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);// 你也可以使用xml格式 save << "words" << words; save << "number" << n; save << "matrix" << m; save.release(); //存儲完畢 cout << "finish storing" << endl;加載
//加載數據,類似Python字典的用法,創建加載器 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;}演示結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選