您好,登錄后才能下訂單哦!
使用C++實現(xiàn)保存txt文件?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(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; }
實際應用
一個det.txt存的都是目標檢測的信息
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
(抽取單個目標展示)
幀號 目標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 解析其中的數(shù)據(jù)
這里的解析沒有用到字符串分割等,而是利用了istringstream對象直接將不同類型的數(shù)據(jù)導給不同的變量。
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 (抽取單個目標展示) 幀號 目標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;//存放逗號,沒有實際用處,去除數(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)容,你們掌握使用C++實現(xiàn)保存txt文件的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。