C++使用标准库方便地读入csv逗号分隔格式的数据

编写程序过程中常常会碰到读入文件中的数据的操作,这里使用标准库方便地读取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;
}

        

猜你喜欢

转载自blog.csdn.net/u013238941/article/details/129124089