C++ 结合 Boost:40行代码读写和处理 txt 文件

来源:互联网 发布:windows phone安卓软件 编辑:程序博客网 时间:2024/05/24 15:38

C++ 结合 Boost:40行代码读写和处理 txt 文件

#include <iostream>#include <fstream>#include <string>#include <vector>#include <boost/tokenizer.hpp>using namespace std;using namespace boost;int main (){    string stringLine;    ifstream infile;    vector<string> tempLine;    vector<string> rootName;    string tempName;    ofstream fout( "rootFilterData.txt", ios::app);    infile.open ("All Simple_Test.txt");    while( !infile.eof() ) // To get you all the lines.    {        getline(infile,stringLine); // Saves the line in stringLine.        char_separator<char> sep(" ");        tokenizer<char_separator<char>> tok(stringLine, sep);        for(tokenizer<char_separator<char>>::iterator beg=tok.begin(); beg!=tok.end(); ++beg)        {            tempLine.push_back( *beg );        }        if ( tempLine[0][0] == 'B')        {   tempName = tempLine[0].substr(0,tempLine[0].length() - 2);            rootName.push_back( tempName );            fout << tempName << endl;        }        fout << tempLine[1] << "  "<< tempLine[2] << "  "<< tempLine[3] << endl;        tempLine.clear();    }    infile.close();}


读取文件:
B2C101   -27.77   -1.65  29.88 154.11  21.38 -104.86
01   -27.57   -2.37  29.98 176.30  34.07  -80.61
01   -27.20   -2.76  30.12 179.84  41.28  -73.31
01   -26.30   -3.08  30.46 166.72  57.57  -78.20
01   -25.72   -3.26  30.86 175.73   3.15 -106.02
01   -24.74   -3.80  31.67 172.26  34.96  -79.90
01   -24.12   -4.22  31.90 174.86  33.39  -77.98
B2A101   -28.20   -0.34  29.57 149.64  19.00  -92.83
01   -28.42   -1.40  29.91 153.32  17.30  -85.85
01   -27.82   -2.46  30.98 157.39  -10.26  -62.65
01   -27.08   -3.75  32.21 173.38  25.30  -86.98
01   -26.77   -4.23  33.02 173.91  10.51 -103.79
01   -26.13   -4.76  34.16 176.14   -8.42 -127.52
01   -12.36  -14.60  45.89 171.40  -26.53 -121.81
B2A201   -28.65   -2.00  29.23 142.44  -53.83  -48.03
01   -28.02   -3.45  28.01 173.60  -59.29  -81.29
01   -27.64   -3.97  27.07 -174.98  -57.37  -87.45
01   -27.24   -4.43  26.36 -164.60  -56.27  -93.43
01   -26.80   -4.88  25.59 -163.81  -58.39  -88.78
01   -26.43   -5.12  25.17 -164.37  -61.96  -89.71
01   -25.96   -5.62  24.40 -158.32  -64.69  -95.05
01   -25.46   -5.85  23.64 -151.02  -70.79 -107.60
B2D101   -28.07   -1.52  28.74 116.77  -68.18  -16.87
01   -27.43   -2.01  28.38 166.19  -37.94  -68.38
01   -26.48   -3.18  27.73 161.65  -44.48  -77.09
01   -26.06   -3.64  27.49 164.83  -45.44  -79.90
01   -25.67   -4.05  27.06 165.46  -45.04  -79.63
01   -24.89   -4.86  26.64 166.02  -44.88  -81.50
01   -24.54   -5.41  26.22 173.88  -46.89  -85.80


输出文件:
B2C1
-27.77  -1.65  29.88
-27.57  -2.37  29.98
-27.20  -2.76  30.12
-26.30  -3.08  30.46
B2A1
-28.20  -0.34  29.57
-28.42  -1.40  29.91
-27.82  -2.46  30.98
-27.08  -3.75  32.21
-26.77  -4.23  33.02
B2C2
-28.26  -2.14  29.25
-27.66  -3.26  29.21
-26.53  -4.36  29.52
-25.84  -4.92  29.58
-24.98  -5.61  29.56
-24.12  -6.49  29.72
B2A2
-28.65  -2.00  29.23
-28.02  -3.45  28.01
-27.64  -3.97  27.07
-27.24  -4.43  26.36
-26.80  -4.88  25.59
-26.43  -5.12  25.17
B2D1
-28.07  -1.52  28.74
-27.43  -2.01  28.38
-26.48  -3.18  27.73
-26.06  -3.64  27.49
-25.67  -4.05  27.06