C++ uses the standard library to easily read data in csv comma-separated format

In the process of writing a program, the operation of reading data in the file is often encountered. Here, the standard library is used to conveniently read the data file in CSV format. The idea is to read each line of the file as a string, then replace the commas in the string with spaces, convert the string to a character stream type, and then use the input operator to read.

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;
}

        

Guess you like

Origin blog.csdn.net/u013238941/article/details/129124089