C++保存txt文件實(shí)現(xiàn)方法代碼實(shí)例
簡(jiǎn)單示例
#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;
}
實(shí)際應(yīng)用
一個(gè)det.txt存的都是目標(biāo)檢測(cè)的信息
1TXT數(shù)據(jù)存放數(shù)據(jù)格式
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
(抽取單個(gè)目標(biāo)展示)
幀號(hào) 目標(biāo)ID x y w h 準(zhǔn)確度
1,-1,291.557,192.468,39.494,119.227,0.995128,-1,-1,-1

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

程序功能:
1 讀取原txt
2 存到另一個(gè)txt里面
3 解析其中的數(shù)據(jù)
這里的解析沒有用到字符串分割等,而是利用了istringstream對(duì)象直接將不同類型的數(shù)據(jù)導(dǎo)給不同的變量。
istringstream對(duì)象解析的簡(jiǎn)單示例
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
istringstream istr;
istr.str("1 56.7");
//上述兩個(gè)過程可以簡(jiǎn)單寫成 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;
}
上例中,構(gòu)造字符串流的時(shí)候,空格會(huì)成為字符串參數(shù)的內(nèi)部分界,例子中對(duì)a,b對(duì)象的輸入"賦值"操作證明了這一點(diǎn),字符串的空格成為了整型數(shù)據(jù)與浮點(diǎn)型數(shù)據(jù)的分解點(diǎn),利用分界獲取的方法我們事實(shí)上完成了字符串到整型對(duì)象與浮點(diǎn)型對(duì)象的拆分轉(zhuǎn)換過程。
工程代碼:
#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數(shù)據(jù)存放數(shù)據(jù)格式
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
(抽取單個(gè)目標(biāo)展示)
幀號(hào) 目標(biāo)ID x y w h 準(zhǔn)確度
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;//存放讀取出的單個(gè)目標(biāo)信息
istringstream ss;
vector<TrackingBox> detData;
char ch;//存放逗號(hào),沒有實(shí)際用處,去除數(shù)據(jù)間的逗號(hào)
float tpx, tpy, tpw, tph;
while (getline(myfile, detLine)) //按行讀取字符串
{
TrackingBox tb;
ss.str(detLine);
ss >> tb.frame >> ch >> tb.id >> ch;// ch用來存放逗號(hào)
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;
}
運(yùn)行之后
重新按行存放輸出到out.txt

解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中declspec(dllexport)和declspec(dllimport)?的用法介紹
這篇文章介紹了C++中declspec(dllexport)和declspec(dllimport)?的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
C++ 中的INT_MAX,INT_MIN數(shù)值大小操作
這篇文章主要介紹了C++ 中的INT_MAX,INT_MIN數(shù)值大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
如何判斷一個(gè)整數(shù)的二進(jìn)制中有多少個(gè)1
本篇文章是對(duì)如何判斷一個(gè)整數(shù)的二進(jìn)制中有多少個(gè)1的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

