getline 没有得到第一个令牌

问题描述

我正在尝试读取一个 CSV 文件,问题是每次程序读取一行并尝试用逗号分割它时,它显然会跳过第一个标记。这是为什么?

void readCSV(string path) {
    ifstream file;
    string line,cell;
    stringstream ss;
    
    file.open(path);
    if (file.fail()) {
        throw runtime_error("Cannot read file");
    }

    while (getline(file,line)) {
        ss << line;
        while (getline(ss,cell,',')) {
            cout << cell << " ";
        }
        cout << endl;
        ss.str(string());
        ss.clear();
    }
    
    file.close();
}

CSV 文件内容

a,b,c,d,e
1,2,3,4,5

输出

  b c d e
  2 3 4 5

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)