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

C++ 结合 Boost:40行代码读写和处理 txt 文件
【Boost|C++ 结合 Boost(40行代码读写和处理 txt 文件)】

#include #include #include #include #include using namespace std; using namespace boost; int main () { string stringLine; ifstream infile; vector tempLine; vector 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 sep(" "); tokenizer> tok(stringLine, sep); for(tokenizer>::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.6529.88 154.1121.38 -104.86
01-27.57-2.3729.98 176.3034.07-80.61
01-27.20-2.7630.12 179.8441.28-73.31
01-26.30-3.0830.46 166.7257.57-78.20
01-25.72-3.2630.86 175.733.15 -106.02
01-24.74-3.8031.67 172.2634.96-79.90
01-24.12-4.2231.90 174.8633.39-77.98
B2A101-28.20-0.3429.57 149.6419.00-92.83
01-28.42-1.4029.91 153.3217.30-85.85
01-27.82-2.4630.98 157.39-10.26-62.65
01-27.08-3.7532.21 173.3825.30-86.98
01-26.77-4.2333.02 173.9110.51 -103.79
01-26.13-4.7634.16 176.14-8.42 -127.52
01-12.36-14.6045.89 171.40-26.53 -121.81
B2A201-28.65-2.0029.23 142.44-53.83-48.03
01-28.02-3.4528.01 173.60-59.29-81.29
01-27.64-3.9727.07 -174.98-57.37-87.45
01-27.24-4.4326.36 -164.60-56.27-93.43
01-26.80-4.8825.59 -163.81-58.39-88.78
01-26.43-5.1225.17 -164.37-61.96-89.71
01-25.96-5.6224.40 -158.32-64.69-95.05
01-25.46-5.8523.64 -151.02-70.79 -107.60
B2D101-28.07-1.5228.74 116.77-68.18-16.87
01-27.43-2.0128.38 166.19-37.94-68.38
01-26.48-3.1827.73 161.65-44.48-77.09
01-26.06-3.6427.49 164.83-45.44-79.90
01-25.67-4.0527.06 165.46-45.04-79.63
01-24.89-4.8626.64 166.02-44.88-81.50
01-24.54-5.4126.22 173.88-46.89-85.80


输出文件:
B2C1
-27.77-1.6529.88
-27.57-2.3729.98
-27.20-2.7630.12
-26.30-3.0830.46
B2A1
-28.20-0.3429.57
-28.42-1.4029.91
-27.82-2.4630.98
-27.08-3.7532.21
-26.77-4.2333.02
B2C2
-28.26-2.1429.25
-27.66-3.2629.21
-26.53-4.3629.52
-25.84-4.9229.58
-24.98-5.6129.56
-24.12-6.4929.72
B2A2
-28.65-2.0029.23
-28.02-3.4528.01
-27.64-3.9727.07
-27.24-4.4326.36
-26.80-4.8825.59
-26.43-5.1225.17
B2D1
-28.07-1.5228.74
-27.43-2.0128.38
-26.48-3.1827.73
-26.06-3.6427.49
-25.67-4.0527.06

    推荐阅读