要读取的 .txt 文件类似这种data.txt
:
1654500.311012 -0.791258 2.34435 -1.18087 -0.277263 0.291918 0.915365 -0.0040246
1654500.311026 -0.79126 2.34427 -1.18093 -0.277245 0.291982 0.915349 -0.00402986
1654500.311039 -0.791461 2.34446 -1.18058 -0.277135 0.291837 0.915429 -0.00403949
1654500.311053 -0.791295 2.34429 -1.18089 -0.277276 0.291919 0.91536 -0.0040508
1654500.311067 -0.791507 2.34425 -1.18074 -0.277172 0.291917 0.915392 -0.00411865
1654500.311080 -0.791305 2.34433 -1.18086 -0.277298 0.291872 0.915368 -0.0040603
1654500.311094 -0.791258 2.34444 -1.18078 -0.277217 0.291909 0.915382 -0.00398512
1654500.311108 -0.791347 2.34443 -1.18071 -0.277146 0.291925 0.915398 -0.00400403
1654500.311121 -0.791324 2.34453 -1.18066 -0.277194 0.291825 0.915415 -0.00399121
1654500.311135 -0.791396 2.34438 -1.18072 -0.277141 0.291934 0.915396 -0.00402841
1654500.311149 -0.791352 2.34434 -1.18079 -0.277259 0.291864 0.915383 -0.0040705
1654500.311163 -0.791372 2.34445 -1.18068 -0.277184 0.29186 0.915407 -0.00401616
1654500.311176 -0.791417 2.34433 -1.18075 -0.277202 0.291896 0.91539 -0.00407268
1654500.311190 -0.791318 2.34432 -1.18084 -0.277187 0.291985 0.915366 -0.00402245
1654500.311204 -0.79126 2.34427 -1.18093 -0.277324 0.291908 0.915349 -0.00405644
1654500.311217 -0.791523 2.34432 -1.18067 -0.277114 0.291923 0.915408 -0.00408464
1654500.311231 -0.791457 2.34449 -1.18056 -0.277175 0.291794 0.915431 -0.00404382
1654500.311245 -0.791331 2.34431 -1.18084 -0.277241 0.291916 0.915372 -0.00405106
1654500.311258 -0.791265 2.34433 -1.18087 -0.277223 0.291976 0.915358 -0.00401219
1654500.311272 -0.791159 2.34419 -1.18109 -0.277265 0.292081 0.915312 -0.00401226
1654500.311286 -0.791386 2.34444 -1.18067 -0.277133 0.291899 0.91541 -0.00401598
1654500.311300 -0.791367 2.34438 -1.18074 -0.277174 0.291914 0.915393 -0.00402691
1654500.311313 -0.791483 2.34431 -1.18068 -0.277092 0.291962 0.915402 -0.00406478
1654500.311327 -0.791388 2.34407 -1.18099 -0.277289 0.291987 0.915334 -0.00414426
1654500.311341 -0.791347 2.34434 -1.1808 -0.277164 0.291967 0.915379 -0.00402623
以上数据文本有如下特点,每行数据个数相同,数据之间采用空格隔开,读取该数据文件的方式有逐词读取、逐行读取。
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
// 逐词读取,按照空格进行分隔
ifstream fin("data.txt");
string s;
while(fin>>s)
{
cout<<s<<endl;
}
fin.close();
s.clear();
cout<<"==============================="<<endl;
// 逐行读取,按照每行结束的回车区分
fin.open("data.txt");
while(getline(fin,s))
{
cout<<s<<endl;
}
fin.close();
s.clear();
return 0;
}
逐行读取操作数据不灵活,逐词读取数据体量又太大,那么自然想到先逐行,再从每一行中采用逐词读取的方式提取到感兴趣的数据。
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin;
istringstream iss;
string s;
double t;
vector<double> tube;
int count=0;
// 逐行读取,将每一行数据读取到字符串 s 中
fin.open("data.txt");
while(getline(fin, s))
{
iss.clear();
iss.str(s);
// 逐词读取,遍历每一行中的每个词 t
while(iss>>t)
{
cout<<t<<" ";
}
cout<<endl;
}
return 0;
}
按照上述方式,就可以提取到你感兴趣的数据,比如抽取该data.txt
文件的第二三四列数据,并存放到xyz_pos.csv
文件中(方便 Matlab 绘制曲线用):
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin;
ofstream outFile; // 2、创建流对象
istringstream iss;
string s;
double t;
vector<double> tube_x;
vector<double> tube_y;
vector<double> tube_z;
int count=0;
// 按行读取,将每一行数据读取到字符串 s 中
fin.open("data.txt");
while(getline(fin, s))
{
iss.clear();
iss.str(s);
// 逐词读取,操作/提取 你感兴趣的数据
while(!iss.eof())
{
iss>>t;
count +=1;
if(count%8==2)
{
tube_x.push_back(t);
}
if(count%8==3)
{
tube_y.push_back(t);
}
if(count%8==4)
{
tube_z.push_back(t);
}
}
count = 0;
}
int num = tube_x.size();
cout << "num=" << num << endl;
for(int j=0;j<num;j++)
{
cout << "tube_x:" << tube_x[j] << " " << "tube_y:" << tube_y[j] << " " << "tube_z:" << tube_z[j] << endl;
outFile.open("xyz_pos.csv", ios::app);
outFile << tube_x[j] << "," << tube_y[j] << "," << tube_z[j] << "\n";
outFile.close();
}
return 0;
}
会在同级目录下生成 xyz_pos.csv
文件