1
計數以CSV列數我下面將溶液代碼由洛基阿斯塔上迭代使用C++
How can I read and parse CSV files in C++?
我怎樣才能在主函數寫一個迭代從計數的列數CSV標題
int main()
{
std::ifstream file("plop.csv");
for(CSVIterator loop(file); loop != CSVIterator(); ++loop)
{
//Instead of printing the 4th element as shown below, I want to print all the
//columns and thus determine the number of columns
//std::cout << "4th Element(" << (*loop)[3] << ")\n";
}
}
下面是一個CSV文件示例頭,我與
cmd, id, addr, qos, len, lock, prot, burst, size, cache, user, duser, dstrb, data
工作
我想用迭代器或某些for循環打印出來,並確定列數在這種情況下是14
我添加代碼到您的解決方案打印的列!看看它是否有意義 – user2979872
@ user2979872你有它的要點。看到我的最新版本。 – ssell
@ user2979872另請注意,CSV文件使用逗號分隔符(因此名稱爲逗號分隔值)。所以你的第一列'cmd'沒有額外的空間,因爲後面的那些空間(因爲CSV解析器只能用逗號分割)。我還會爭辯說''row'比'loop'更好,但我明白這是您引用的原始答案中使用的名稱。 – ssell