将getline与CSV一起使用
Using getline with CSV
我花了将近4个小时试图解决这个问题。。。
我有一个超过100行的文本文件。每行有4个值,用逗号分隔。我希望能够提取每个值并将其保存到一个变量(v1…v4)中
我使用了for循环,因为我不会读取文件的全部内容。我只是想让1暂时工作。
到目前为止,我只读了一行。我现在只需要打破这场争吵。这是我的Uni作业,我不允许使用任何boost或tokeniser类。只需getline和其他基本命令。
我有这个代码:
// Read contents from books.txt file
ifstream theFile("fileName.txt");
string v1, v2, v3, v4, line;
for (int i = 0; i < 1; i++) {
getline(theFile, line, 'n');
cout << line << endl; // This part works fine
getline(line, v1, ","); // Error here
cout << v1 << endl;
getline(line, v2, ","); // Error here
cout << v2 << endl;
getline(line, v3, ","); // Error here
cout << v3 << endl;
getline(line, v4, 'n'); // Error here
cout << v4 << endl;
}
theFile.close();
我得到的错误是-error:调用"getline(std::string&,std::string&,const char[2])没有匹配的函数
我该怎么解决这个问题?
getline的分隔符是一个字符。您使用了表示字符串的双引号","
(因此,编译器错误指示您使用了char[2]
——字符串文字包含额外的"nul"字符)。
单字符值使用单引号表示:
getline(myFile, v1, ',');
edit-我刚刚注意到您正在传递一个字符串作为第一个参数,getline不支持该参数(它不允许您直接从字符串中检索令牌)。您可能想将字符串填充到stringstream
中,而不是中
#include <sstream>
// etc ...
std::string csv = "the,cat,sat,on,the,mat";
std::istringstream buffer( csv );
std::string token;
while( std::getline( buffer, token, ',' ) )
{
std::cout << token << std::endl;
}
根据此页面,您必须使用std::istream
作为第一个参数来调用getline
。
但line
属于std::string
型。您应该传递一个std::istream
,它可以可以通过从CCD_ 10创建CCD_。这可能奏效:
string v1, v2, v3, v4, line;
istringstream line_stream;
for (int i = 0; i < 1; i++) {
getline(theFile, line, 'n');
cout << line << endl;
line_stream.str(line); // set the input stream to line
getline(line_stream, v1, ","); // reads from line_stream and writes to v1
...
如果你想阅读更多关于std::istringstream
的内容,你可以在这里阅读。
此外,根据上面的页面,第三个参数必须是类型char
。但是您传递了","
,它将自动转换为字符串const char[2]
。对于字符,可以使用'c'
。
std::getline:有两个重载
istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );
您的三个调用传递一个文字字符串常量作为第三个参数,其中需要一个char
。使用'
而不是"
作为字符常量。
string filename, text, line;
vector<string> v1;
vector<string> v2;
vector<string> v3;
vector<string> v4;
int i = 0, k = 0, n;
cout << "Enter filename." << endl;
cin >> filename;
cout << "How many lines of text are in the file?" << endl;
cin >> n;
cin.ignore(200, 'n');
ifstream file(filename.c_str());
if (!file) {
cerr << "No such file exists." << endl;
exit(1);
}
cin.ignore(200, 'n');
if (file.is_open()) {
while (file.good()) {
for (k = 0; k < n; k++) { //Loops for as many lines as there are in the file
for (i = 0; i < 4; i++) { //Loops for each comma-separated word in the line
getline(cin, text, ',');
if (i == 0)
v1.push_back(text);
else if (i == 1)
v2.push_back(text);
else if (i == 2)
v3.push_back(text);
else if (i == 3)
v4.push_back(text);
}
}
}
}
file.close();
return 0;
}
- 正在将csv文件读取为双精度矢量
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何选择在 csv 文件中输出的位置
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 将getline与CSV一起使用