c++ 读写文件

txt文件

#include
#include
 
using namespace std;
 
int main()
{
	// *************************写txt文件*******************************
	for(auto &t: res){
        stringstream filename;
        filename << "../up_escalator_track_result_annos/" << t.first << ".txt";
        ofstream OutFile(filename.str());
        for (int j = 0; j < t.second.size(); j++){
            for (int k = 0; k < t.second[j].size(); k++){
                OutFile << t.second[j][k] << " ";
            }
            OutFile << endl;
        }
        OutFile.close();
    }
	// *************************读txt文件*******************************
	ifstream infile(filename.str());
    if(!infile.good()) continue;
    while (getline(infile, line)) {
            vector<string> vStr;
            boost::split( vStr, line, boost::is_any_of( " " ), boost::token_compress_on );
            int x, y, w, h;
            x = stof(vStr[0]) * width;
            y = stof(vStr[1]) * height;
            w = stof(vStr[2]) * width;
            h = stof(vStr[3]) * height;
            cv::rectangle(ori_img, cv::Rect(x, y, w, h), cv::Scalar(255, 0, 255), 2);
    }
	infile.close();
	return 0;
}

json文件

#include 
#include 
#include 
#include 
#include 

using namespace std;
const std::string file_path = "/home/xkp/detection_results/up_escalator_track_result.json";

void read_user() {

    map<string, vector<vector<float>>> res;
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;
    boost::property_tree::read_json<boost::property_tree::ptree>(file_path, root);

    boost::property_tree::ptree trajs_all;
    trajs_all = root.get_child("trajs_all");
    for (boost::property_tree::ptree::iterator it = trajs_all.begin(); it != trajs_all.end(); ++it) {
        boost::property_tree::ptree value = it->second;
        boost::property_tree::ptree::iterator it2 = value.begin();
        advance(it2, 1);
        boost::property_tree::ptree value2 = it2->second;
        for (boost::property_tree::ptree::iterator it3 = value2.begin(); it3 != value2.end(); ++it3) {
            boost::property_tree::ptree value3 = it3->second;
            string frame_idx = value3.get<string>("frame_idx");
//            cout << frame_idx << endl;
            float x = value3.get_child("det").get_child("loc").get<float>("x");
            float y = value3.get_child("det").get_child("loc").get<float>("y");
            float w = value3.get_child("det").get_child("loc").get<float>("width");
            float h = value3.get_child("det").get_child("loc").get<float>("height");
//            cout << x << " " << y << " " << w << " " << h << endl;
            vector<float> tmp = {x, y, w, h};
            res[frame_idx].push_back(tmp);
        }
    }
    for(auto &t: res){
        stringstream filename;
        filename << "../up_escalator_track_result_annos/" << t.first << ".txt";
        ofstream OutFile(filename.str());
        for (int j = 0; j < t.second.size(); j++){
            for (int k = 0; k < t.second[j].size(); k++){
                OutFile << t.second[j][k] << " ";
            }
            OutFile << endl;
        }
        OutFile.close();
    }
}

int main() {
    read_user();
}

你可能感兴趣的:(c++,c++,开发语言,算法)