C++ 将所需内容存储到txt文本中

前言:

题外话:今天911,首先想到了"911事件",转眼间11年过去了,感叹时间之快!

话说,在CV领域,特别是目标检测任务中,计算算法的AP值是一件基本任务。

特别地,在使用C++做前向推理的时候,对其预测结果做AP计算的时候,往往可以将其预测结果先存储到txt文本中,然后将其结果转为计算AP所需要的数据格式,最终再使用计算AP的脚本进行计算。

这里主要是介绍一下在C++中如何将检测结果存储在txt的方法,肥西勿喷。

参考代码:

方法一:

/*
Data:2020-09-11
Author: william
Function:在遍历文件夹中的图像进行算法预测的时候,可以将其结果按行存储到txt文件中.
The test environment:Visual Studio 2017
*/

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
using namespace std;

int main() {

	FILE *fp;
	int a = 1;
	string figure_a = "The Heat";
	string event = "defeated";
	string figure_b = "the Bucks";
	string times = "in the 2020 NBA playoff quarterfinals.";
	stringstream strStream;
	fp = fopen(R"(D:\\vs2017_Project\\11th_save_info_txt\\ConsoleApplication1\\ConsoleApplication1\\calivalue.txt)", "w");

	while (a < 4) {

		strStream << "第"<< a << "次:"<< figure_a << " " << event << " " << figure_b << " " << times << endl;
		string s = strStream.str();
		fputs(s.c_str(), fp);
		strStream.str("");
		a++;
	}
	fclose(fp);
	cout << "存储成功!" << endl;
	system("pause");
}

方法二:

#include
#include
#include
using namespace std;
	
int main()
{
	ofstream os;     //创建一个文件输出流对象
	os.open("D:\\vs2017_Project\\11th_save_info_txt\\ConsoleApplication1\\ConsoleApplication1\\books.txt");//将对象与文件关联
	string figure_a = "The Heat";
	string event = "defeated";
	string figure_b = "the Bucks";
	string times = "in the 2020 NBA playoff quarterfinals.";
	string result;
	int a = 1;
	while (a < 4) {
		result = "第" + to_string(a) + "次说:" + figure_a + " " + event + " " + figure_b + " " + times + " " + "\n";
		os << result;   //将输入的内容放入txt文件中
		a++;
	}
	os.close();
	return 0;
}

 

你可能感兴趣的:(C++)