json文件生成by 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];
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值