1

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

我得到的错误是 - 错误:没有匹配函数调用 'getline(std::string&, std::string&, const char [2])

我怎样才能解决这个问题?

4

4 回答 4

2

The delimiter for getline is a character. You have used double-quote marks "," which represent a string (hence why the compiler error indicates that you've used char[2] - string literals contain an additional 'nul' character).

Single character values are represented using single quotes instead:

getline(myFile, v1, ',');

edit - I've just noticed you're passing a string as the first parameter, which getline doesn't support (it won't let you retrieve tokens directly from a string). You probably wanted to stuff the string into a stringstream instead

#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;
}
于 2012-09-02T09:09:01.327 回答
2

According to this page, you have to call getline with std::istream as the first parameter.

But line is of type std::string. You should pass a std::istream instead, which can be done by creating an std::istringstream from line. This could work:

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
...

If you want to read more about std::istringstream, you can do that here.

Also, according to the above page, the third parameter has to be of type char. But you pass "," which will automatically converted to const char[2], a string. For characters, you use 'c'.

于 2012-09-02T09:11:22.333 回答
0
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;
}
于 2012-09-02T10:58:03.313 回答
0

std::getline 有两个重载:

istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );

您的三个调用将文字字符串常量作为第三个参数传递,其中char需要一个。使用'而不是"用于字符常量。

于 2012-09-02T11:00:34.020 回答