C++保存txt文件實現(xià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; }
實際應(yīng)用
一個det.txt存的都是目標(biāo)檢測的信息
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
(抽取單個目標(biā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 存到另一個txt里面
3 解析其中的數(shù)據(jù)
這里的解析沒有用到字符串分割等,而是利用了istringstream對象直接將不同類型的數(shù)據(jù)導(dǎo)給不同的變量。
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; }
上例中,構(gòu)造字符串流的時候,空格會成為字符串參數(shù)的內(nèi)部分界,例子中對a,b對象的輸入"賦值"操作證明了這一點,字符串的空格成為了整型數(shù)據(jù)與浮點型數(shù)據(jù)的分解點,利用分界獲取的方法我們事實上完成了字符串到整型對象與浮點型對象的拆分轉(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 (抽取單個目標(biā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;//存放讀取出的單個目標(biāo)信息 istringstream ss; vector<TrackingBox> detData; char ch;//存放逗號,沒有實際用處,去除數(shù)據(jù)間的逗號 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
解析
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中declspec(dllexport)和declspec(dllimport)?的用法介紹
這篇文章介紹了C++中declspec(dllexport)和declspec(dllimport)?的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04C++ 中的INT_MAX,INT_MIN數(shù)值大小操作
這篇文章主要介紹了C++ 中的INT_MAX,INT_MIN數(shù)值大小操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03