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