json檔案生成by C++
阿新 • • 發佈:2021-01-16
技術標籤:C++
#include<fstream> #include <vector> #include<iostream> #include <io.h> #include<CString> #include<string> #include <stdlib.h> #include <afx.h> #include "Windows.h" #include<stdio.h> using namespace std; using std::string; #pragma warning(disable:4996); void getSubdirs(string, vector<string>&); void getPicture(string, vector<string>&, string); void getAllFiles(string, string, vector<string>&, string); void output(ifstream&); void outputFirst(ifstream&); int main(int argc, char* argv[]) { char ExeFile[200]; GetModuleFileName(NULL, ExeFile, 200); //得到當前exe檔案路徑名存在ExeFile中 CString str = ExeFile;//將存放的路徑賦給字串str int pos = str.ReverseFind('\\');//查詢倒數最後一個“\\”符號 str = str.Left(pos + 1);//去除exe名,只保留路徑 string filePath = str.GetBuffer(0);//當前資料夾路徑 //string filePath = "D:\\GOT-10k";//初始路徑 vector<string> files;//儲存資料夾名 getSubdirs(filePath, files);//獲取各資料夾名 std::cout << "{";//第一個大括號 for (int i = 0; i < files.size(); i++) { if (i)//第一個沒有空格 std::cout << " "; std::cout << "\"" << files[i]<<"\": "; std::cout << "{" << "\"video_dir\": "; std::cout << "\"" << files[i] << "\"" << ", "; std::cout << "\"img_names\": ["; vector<string>files2;//儲存資料夾下的jpg檔案 getAllFiles(filePath, files[i], files2, ".jpg");//獲取jpg檔名 for (int j = 0; j < files2.size(); j++) { std::cout << "\"" << files[i] << "/" << files2[j] << "\""; if (j != (files2.size() - 1))//最後一個不輸出逗號 std::cout << ", "; } std::cout << "], ";//jpg輸出完畢 std::cout << "\"init_rect\": "; std::cout << "["; //vector<string>files3;//儲存資料夾下的txt檔案 //getAllFiles(filePath, files[i], files3, ".txt");//獲取txt檔名 string txtfile; txtfile = filePath; txtfile.append("\\" + files[i] + "\\" + "groundtruth_rect.txt"); ifstream readFile(txtfile);//開啟txt outputFirst(readFile);//輸出txt中的內容 std::cout << "], "; readFile.close();//關閉檔案 std::cout << "\"gt_rect\": [["; ifstream readFile2(txtfile);//開啟txt outputFirst(readFile2);//輸出txt中的內容 std::cout << "],"; readFile2.close();//關閉檔案 for (int k = 0; k < files2.size() - 2; k++) { std::cout << " [0, 0, 0, 0],"; } std::cout << " [0, 0, 0, 0]]}";//最後一個0 if (i != files.size() - 1) std::cout << ","; } std::cout << "}"; /*system("pause");*/ return 0; } void getSubdirs(std::string path, std::vector<std::string>& files) { long long hFile = 0; struct _finddata_t fileinfo; std::string p; if ((hFile = _findfirst(p.assign(path).append("/*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR))//如果是資料夾 { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) files.push_back(fileinfo.name); } else { ; } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } void getAllFiles(string path, string name, vector<string>& files, string fileType) { path.append("\\" + name);// 檔案控制代碼 long long hFile = 0;// 檔案資訊 struct _finddata_t fileinfo2; string p; if ((hFile = _findfirst(p.assign(path).append("\\*" + fileType).c_str(), &fileinfo2)) != -1) { do {// 儲存檔案的路徑 files.push_back(fileinfo2.name); } while (_findnext(hFile, &fileinfo2) == 0); //尋找下一個,成功返回0,否則-1 _findclose(hFile); } } void output(ifstream& readFile) { vector<int>txtfile; while (!readFile.eof()) { string buff; while (getline(readFile, buff)) { vector<double> nums; // string->char * char *s_input = (char *)buff.c_str(); const char * split = ","; // 以‘,’為分隔符拆分字串 char *p = strtok(s_input, split); double a; while (p != NULL) { // char * -> int a = atof(p); //cout << a << endl; nums.push_back(a); p = strtok(NULL, split); }//end while for (int l = 0; l < nums.size() - 1; l++) { std::cout << nums[l] << ", "; } std::cout << nums[nums.size() - 1]; } } } void outputFirst(ifstream& readFile) { string buff; getline(readFile, buff); vector<double> nums; // string->char * char *s_input = (char *)buff.c_str(); const char * split = " \t"; // 以‘ ’為分隔符拆分字串 char *p = strtok(s_input, split); double a; while (p != NULL) { // char * -> int a = atof(p); //cout << a << endl; nums.push_back(a); p = strtok(NULL, split); }//end while for (int l = 0; l < nums.size() - 1; l++) { std::cout << nums[l] << ", "; } std::cout << nums[nums.size() - 1]; }