编写程序过程中常常会碰到读入文件中的数据的操作,这里使用标准库方便地读取CSV格式的数据文件。思路就是读取文件的每一行为一个string,然后将string中的逗号替换为空格,再将string转换为字符流类型,然后使用输入运算符读取。
std::ifstream fin("f:/temp/data.txt");
while(!fin.eof()){
std::string stringline;
std::getline(fin, stringline);
std::replace(stringline.begin(), stringline.end(), ',', ' ');
std::stringstream strstream(stringline);
double tx, ty, tz, qx, qy, qz, qw; // 读入位移和四元数
strstream >> qx >> qy >> qz >> qw >> tx >> ty >> tz;
std::cout<<"quaternions and translation "<<qx <<", "<<qy<<", "<<qz<<", "<<qw<<", "<<tx<<", "<<ty<<", "<<tz<<std::endl;
}