知識點5 C++保存txt文件


 

簡單示例

#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	ifstream myfile("in.txt");
	ofstream outfile("out.txt", ios::app); //ios::app指追加寫入
	string temp;
	
	while (getline(myfile, temp)) //按行讀取字符串 
	{
		outfile << temp<<endl;//寫文件
	
	}
	myfile.close();
	outfile.close();
	while (1);
	return 0;
}

實際應用

一個det.txt存的都是目標檢測的信息

1TXT數據存放數據格式
1,-1,281.931,187.466,79.93,209.537,0.997784,-1,-1,-1
1,-1,56.6878,144.225,93.5572,295.907,0.997601,-1,-1,-1
1,-1,378.618,188.922,166.431,234.127,0.995973,-1,-1,-1
1,-1,203.983,207.153,45.553,133.834,0.985409,-1,-1,-1
(抽取單個目標展示)
幀號 目標ID x y w h 准確度
1,-1,291.557,192.468,39.494,119.227,0.995128,-1,-1,-1

 

 

(雖然看起來很亂,而且沒有換行,但是程序按照行讀取還是能分開??? )

 

 程序功能:

1 讀取原txt

2 存到另一個txt里面

3 解析其中的數據

  這里的解析沒有用到字符串分割等,而是利用了 istringstream對象直接將不同類型的數據導給不同的變量。

istringstream對象解析的簡單示例

#include <iostream>
#include <sstream>
 
using namespace std;
 
int main()
{
	istringstream istr;
	istr.str("1 56.7");
	//上述兩個過程可以簡單寫成 istringstream istr("1 56.7");
	cout << istr.str() << endl;
	int a;
	float b;
	istr >> a;
	cout << a << endl;
	istr >> b;
	cout << b << endl;
	return 0;
}

  上例中,構造字符串流的時候,空格會成為字符串參數的內部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點,字符串的空格成為了整型數據與浮點型數據的分解點,利用分界獲取的方法我們事實上完成了字符串到整型對象與浮點型對象的拆分轉換過程。

 

工程代碼:

 

 

#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>

#include "opencv2/video/tracking.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

typedef struct TrackingBox
{
	int frame;
	int id;
	Rect_<float> box;
}TrackingBox;




/*
1TXT數據存放數據格式 
1,-1,281.931,187.466,79.93,209.537,0.997784,-1,-1,-1
1,-1,56.6878,144.225,93.5572,295.907,0.997601,-1,-1,-1
1,-1,378.618,188.922,166.431,234.127,0.995973,-1,-1,-1
1,-1,203.983,207.153,45.553,133.834,0.985409,-1,-1,-1
(抽取單個目標展示)
幀號 目標ID  x  y  w  h  准確度
1,-1,291.557,192.468,39.494,119.227,0.995128,-1,-1,-1

*/
void TXTtoBOX() {
	string  inFileName  = "det.txt";
	string  outFileName = "out.txt";

	ifstream myfile(inFileName);
	ofstream outfile(outFileName, ios::app); //ios::app指追加寫入


	if (!myfile.is_open())
	{
		cerr << "Error: can not find file " << inFileName << endl;
		return;
	}
	if (!outfile.is_open())
	{
		cerr << "Error: can not find file " << outFileName << endl;
		return;
	}

	string detLine;//存放讀取出的單個目標信息
	istringstream ss;
	vector<TrackingBox> detData;
	char ch;//存放逗號,沒有實際用處,去除數據間的逗號
	float tpx, tpy, tpw, tph;

	while (getline(myfile, detLine)) //按行讀取字符串 
	{
		TrackingBox tb;
		ss.str(detLine);
		ss >> tb.frame >> ch >> tb.id >> ch;// ch用來存放逗號
		ss >> tpx >> ch >> tpy >> ch >> tpw >> ch >> tph;
		ss.str("");//清空后面不管

		tb.box = Rect_<float>(Point_<float>(tpx, tpy), Point_<float>(tpx + tpw, tpy + tph));
		detData.push_back(tb);

		outfile << detLine << endl;//寫文件
		cout <<"detLine:"<< detLine << endl;
		cout << "x:"<<tpx << "	y:"<< tpy << "	w:" << tpw << "	h:" << tph  <<endl;

	}
	myfile.close();
	outfile.close();
	while (1);


}
int main()
{
	TXTtoBOX();
	
	return 0;
}

  

運行之后

重新按行存放輸出到out.txt

 

 

解析

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM