CSV文件读取 C++版本

代码

//创建结构体,把读取数据可以放入结构体成员中
struct WAYPOINT{
    double px;
    double py;
    double pz;
    double vx;
    double vy;
    double vz;
    double ax;
    double ay;
    double az;
};

//读取路径点csv函数
void read_waypoint_csv(std::string CSV_PATH)
{	
	std::ifstream _csvInput;
	_csvInput.open(CSV_PATH);
    if(!_csvInput){
        std::cout<<"open error!  Path = "<< CSV_PATH.c_str() <<std::endl;
        return;
    }
    
    //创建路径点容器
    std::vector<WAYPOINT> waypoint_vec;
    std::string str;
	//读取策略:读取一行数据, 跳过第一行
    size_t line_num = 1;
    //开始循环读取数据
    while(getline(_csvInput, str)){
        WAYPOINT waypt;
        std::istringstream iss(str);
        std::string token;

        if(line_num == 1){
            std::cout << iss.str() << std::endl;
        }else{
            getline(iss, token, ',');
            waypt.px = std::stod(token);
            getline(iss, token, ',');
            waypt.py = std::stod(token);
            getline(iss, token, ',');
            waypt.pz = std::stod(token);

            getline(iss, token, ',');
            waypt.vx = std::stod(token);
            getline(iss, token, ',');
            waypt.vy = std::stod(token);
            getline(iss, token, ',');
            waypt.vz = std::stod(token);

            getline(iss, token, ',');
            waypt.ax = std::stod(token);
            getline(iss, token, ',');
            waypt.ay = std::stod(token);
            getline(iss, token, ',');
            waypt.az = std::stod(token);

            waypoint_vec.emplace_back(waypt);
            printf("%5.3f, %5.3f, %5.3f | %5.3f, %5.3f, %5.3f, | %5.3f, %5.3f, %5.3f\n",
                   waypt.px, waypt.py, waypt.pz,
                   waypt.vx, waypt.vy, waypt.vz,
                   waypt.ax, waypt.ay, waypt.az);
        }
        line_num++;
    }
}

你可能感兴趣的:(个人笔记,c++,开发语言)