版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangdashi888/article/details/82350618
通过使用Opencv的封装函数接口”FileStorage“可以很方便的实现xml、yml文件的读取操作。从而方便进行数据的存储。其相应的代码为:
#include"opencv2//opencv.hpp"
#include"opencv2//highgui//highgui.hpp"
using namespace std;
using namespace cv;
typedef struct
{
int x;
int y;
string s;
}test_t;
void data_info_dump(test_t outt, map<string, int>outm, int a1, int a2, string str, vector<int>out)
{
cout << "a1:" << a1 << endl;
cout << "a2:" << a2 << endl;
cout << "str:" << str << endl;
cout << "outt.x" << outt.x << endl;
cout << "outt.y" << outt.y << endl;
cout << "outt.s" << outt.s << endl;
cout << "curry" << outm["curry"] << endl;
cout << "kobe" << outm["kobe"] << endl;
cout << "james" << outm["james"] << endl;
for (int i = 0; i < out.size(); i++)
{
cout << out[i] << endl;
}
}
int main()
{
//文件可以存储为xml、yml.两者的读取方法都一样
FileStorage fs("test.xml", FileStorage::WRITE);
//FileStorage fs("test.yml", FileStorage::WRITE);
int a1 = 2;
char a2 = -1;
string str = "hello sysu!";
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
test_t t = { 3, 4, "hi sysu" };
map<string, int>m;
m["kobe"] = 100;
m["james"] = 99;
m["curry"] = 98;
fs << "int_data" << a1;
fs << "char_data" << a2;
fs << "string_data" << str;
//这个是进行数组的存储,
fs << "array_data" << "[";
for (int i = 0; i < 10; i++)
{
fs << arr[i];
}
fs << "]";
//写入结构体,其值是有对应的键值的
fs << "struct_data" << "{";
fs << "x" << t.x;
fs << "y" << t.y;
fs << "s" << t.s;
fs << "}";
//map的写入
fs << "map_data" << "{";
map<string, int>::iterator it = m.begin();
for (; it != m.end(); it++)
{
fs << it->first << it->second;
}
fs << "}";
fs.release();
//进行数据的读取。
test_t read_t;
map<string, int>readmap;
FileStorage readfs("test.xml", FileStorage::READ);
vector<int>arrvalue;
a1 = (int)readfs["int_data"];
a2 = (int)readfs["cahr_data"];
str = (string)readfs["string_data"];
FileNode arr_node = readfs["array_data"];
FileNodeIterator fni = arr_node.begin();
FileNodeIterator fniEnd = arr_node.end();
int count = 0;
for (; fni != fniEnd; fni++)
{
arrvalue.push_back((int)(*fni));
}
//数据节点,其中通过“[”或者“{”进行存储的数据
FileNode readmap_node = readfs["map_data"];
readmap["curry"] = (int)readmap_node["curry"];
readmap["james"] = (int)readmap_node["james"];
readmap["kobe"] = (int)readmap_node["kobe"];
FileNode readstruct_node = readfs["struct_data"];
read_t.x = (int)readstruct_node["x"];
read_t.y = (int)readstruct_node["y"];
read_t.s = (int)readstruct_node["s"];
//把读取的结果显示到控制台
data_info_dump(read_t, readmap, a1, a2, str, arrvalue);
while (true)
{
}
return 0;
}