Search code examples
c++ifstreamgetline

No matching function for call to 'get line' while exploding


No matching function for call to 'getline' when using this code:

ifstream myfile;
string line;
string line2;

myfile.open("example.txt");
while (! myfile.eof() )
{
    getline (myfile, line);
    getline (line, line2, '|');
    cout<<line2;
}

In the example.txt i have info like this:

1|Name1|21|170
2|Name2|34|168

etc...

I really want to get the line till the | char...

I try few explode functions but they are only in string type but i need:

1st to be int

2nd to be char

3rd and 4th to be float.

It's really complicated that i want to do, and i can't explain it well. I hope someone will understand me.


Solution

  • getline receives as first argument an instance of the template basic_istream. string doesn't meet that requirement.

    You could use stringstream:

    #include <sstream>
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string line;
        string line2;
        ifstream myfile("test/test.txt");
    
        while (getline(myfile, line))
        {
            stringstream sline(line);
            while (getline(sline, line2, '|'))
                cout << line2 << endl;
        }
    
        return 0;
    }