将getline与CSV一起使用

Using getline with CSV

本文关键字:一起 CSV getline      更新时间:2023-10-16

我花了将近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;
}