gpt4 book ai didi

c++ - 在 C++ 中格式化 CSV 文件

转载 作者:行者123 更新时间:2023-11-30 02:49:00 24 4
gpt4 key购买 nike

我正在尝试获取一个简单的 CSV 文件,将其逐行拆分,然后将其打印到控制台。目前我在编译时遇到错误,想知道我是否遗漏了一些明显的东西。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main(int argc , char** argv) {

std::string line;
std::ifstream infile(argv[1]);

if (infile) {

while (getline(infile, line)) {

std::istringstream ss(line);
std::string token;

while(std::getline(ss, token, ",")) {

std::cout << token << "\n";

}

}

}

infile.close();
return 0;

}

我得到的错误如下。

csv.cpp: In function 'int main(int, char**)':
csv.cpp:41:46: error: no matching function for call to 'getline(std::istringstream&,
std::string&, const char [2])'
csv.cpp:41:46: note: candidates are:
In file included from /usr/include/c++/4.7/string:55:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.7/bits/basic_string.tcc:1070:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: deduced conflicting types for parameter '_CharT' ('char' and 'const char*')
In file included from /usr/include/c++/4.7/string:54:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from csv.cpp:21:
/usr/include/c++/4.7/bits/basic_string.h:2792:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2792:5: note: template argument deduction/substitution failed:
csv.cpp:41:46: note: candidate expects 2 arguments, 3 provided

最佳答案

getline 的第三个参数是char,不是char*。使其成为 getline(ss, token, ',') - 注意单引号。

哦,注意 CSV 字段 "like"",""this"(如果您想知道,这是一个值为 like","this)。 CSV 语法远不止这些。

关于c++ - 在 C++ 中格式化 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21638694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com