1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <string>
#include <set>
#include <exception>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/foreach.hpp>
using namespace boost::filesystem;
using namespace std;
struct job
{
std::string data_path;
int address;
void load(const std::string &filename, std::vector<string> &key, std::vector<int> &address);
void save(const std::string &filename, std::vector<string> &key, std::vector<int> &address);
};
int main()
{
// example data
static const int arr[] = {1,2,3,3,4,3,4};
vector<int> Level (arr, arr + sizeof(arr) / sizeof(arr[0]) );
static const char letter[]={'A','B','C','D','E','F','E'};
vector<char> shortname;
for( int i =0; i < sizeof(letter); i++)
{
shortname.push_back(letter[i]);
}
static const int add[] = {123,234,345,456,567,678,789};
vector<int> address (add, add + sizeof(add) / sizeof(add[0]) );
vector<string> key;
// display output
cout << "Level " << "shortname " << " address " << " generated key" << endl;
for (int i =0; i < address.size(); i++)
{
stringstream temp;
temp << Level[i] << shortname[i];
key.push_back(temp.str());
cout << Level[i] << " " << shortname[i] << " " << address[i] << " " << temp.str() << endl;
}
// create instance of job
job *ds = new job;
// path name of where data will be stored.
std::string path("C:\\temp\\hello.txt");
// call save method
ds->save(path.c_str(),key,address);
// clear vector
address.clear();
// call load method
ds->load(path.c_str(),key,address);
cout << endl;
cout << "generated key " << " address" << endl;
// you will have a problem displaying 4E will be overwritten by the last value
// display loaded data
for (int i =0; i < address.size(); i++)
{
cout << key[i] << " " << address[i] << endl;
}
int x;
cin >> x;
}
void job::save(const std::string &filename, std::vector<string> &key, std::vector<int> &address)
{
using boost::property_tree::ptree;
ptree pt;
// loop through the key and address vector
for(int i=0; i < key.size(); i ++)
{
pt.put(key[i],address[i]);
}
// write to file
write_ini( filename, pt );
}
void job::load(const std::string &filename, std::vector<string> &key, std::vector<int> &address)
{
// Create empty property tree object
using boost::property_tree::ptree;
ptree pt;
boost::property_tree::ini_parser::read_ini(filename.c_str(), pt);
// load data
for(int i=0; i < key.size(); i++)
{
address.push_back(pt.get<int>(key[i]));
}
}
|