读取csv文件中的数据,并保存为二维数组。

vector<vector<double>> read_gt_file(string filename){
    ifstream inFile(filename, ios::in);
    string lineStr;
    char delim=',';
    vector<vector<double>> matrix;
    if(inFile.fail()){
        cout <<"Read files fail....."<<endl;
        return matrix;
    while (getline(inFile, lineStr)) {
        stringstream ss(lineStr);
        string str;
        vector<double> lineArray;
        // cut apart by flag
        while (getline(ss, str, delim))
            lineArray.push_back(std::stod(str));
        matrix.push_back(lineArray);
    return matrix;

如果有做slam的需要,当位姿数据的存储格式为TUM格式(具体形式为:)

timestamp x y z q_x q_y q_z q_w

这时读取位姿数据可以使用如下版本:

#include "Eigen/Core"
#include "Eigen/Geometry"
vector<Eigen::Matrix4d> read_gt_file(string filename){
    ifstream inFile(filename, ios::in);
    string lineStr;
    char delim=' ';
    if(inFile.fail()){
        cout <<"Read files fail....."<<endl;
        Eigen::Matrix4d::Identity();
    vector<Eigen::Matrix4d> gt_poses;
    while (getline(inFile, lineStr)) {
        Eigen::Matrix4d gt_pose = Eigen::Matrix4d::Identity();
        stringstream ss(lineStr);
        string str;
        vector<double> lineArray;
        // cut apart by flag
        while (getline(ss, str, delim))
            lineArray.push_back(std::stod(str));
        Eigen::Quaterniond q(lineArray[7], lineArray[4], lineArray[5], lineArray[6]);
        gt_pose.block<3,3>(0, 0) = q.toRotationMatrix();
        gt_pose.block<3,1>(0, 3) = Eigen::Vector3d(lineArray[1], lineArray[2], lineArray[3]);
        gt_poses.push_back(gt_pose);
    return gt_poses;
// 读取标定文件里的数据  
	ifstream inFile("C:/Users/z84103457/Desktop/more dataBase/1001a_GS0.0001.csv", ios::in);
int main(int argc, char* argv[]) {
    if (argc != 2) {
        printf("Usage: ./read_csv <csv_file_path>\n");
        return 1;
    const char* csv_file_path = argv[1];
    FILE* csv_file = fopen(csv_file_path, "r");
    if (csv_file == NULL) {
        printf("Cannot open CSV file: %s\n", csv_file_path);
        return 1;
    const int MAX_LINE_LENGTH = 1024;
    char line[MAX_LINE_LENGTH];
    while (fgets(line, MAX_LINE_LENGTH, csv_file) != NULL) {
        char* field = strtok(line, ",");
        while (field != NULL) {
            printf("%s ", field);
            field = strtok(NULL, ",");
        printf("\n");
    fclose(csv_file);
    return 0;
这个代码会从命令行参数指定的CSV文件读取数据,然后输出每行的所有字段,各字段之间用空格分隔。你可以将其保存为read_csv.c,然后用GCC编译器编译。
                    CSDN-Ada助手: 
                    非常感谢博主的辛勤创作,您的博客对我们这些技术小白们来说非常有用。这篇博客真的是写得非常好,标题和内容很紧密地联系在一起,开头就引起了我的兴趣。我希望博主能一直保持这样的写作风格,分享更多有价值的知识。非常谢谢您!
为了方便博主创作,提高生产力,CSDN上线了AI写作助手功能,就在创作编辑器右侧哦~(https://mp.csdn.net/edit?utm_source=blog_comment_recall )诚邀您来加入测评,到此(https://activity.csdn.net/creatActivity?id=10450&utm_source=blog_comment_recall)发布测评文章即可获得「话题勋章」,同时还有机会拿定制奖牌。
                频谱泄露、栅栏效应、补零实验
                    m0_56876259: 
                    ÷打做×了
                频谱泄露、栅栏效应、补零实验
                    通信小小白: 
                    0.4x(1/65)=26 0.3x(1/65)=19.5
                频谱泄露、栅栏效应、补零实验
                    OHFOWEHFOW: 
                    这个采样时长是不是整数倍周期,怎么算的?
                ceres位姿图优化
                    P. Gao: 
                    李群到李代数的变换不是只有唯一解,需要限制旋转角的范围在-pi~pi之间才是一一对应的