-2

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.

Nicox
  • 83
  • 3
  • 11
  • I'm guessing that `getline (line, line2, '|');` with a `std::string` for the first parameter is not an actual version of `std::getline`. – crashmstr Dec 15 '14 at 19:26
  • i have: using namespace std; its not that the problem :( – Nicox Dec 15 '14 at 19:30
  • 1
    Um, I'm saying that there is no `getline(string, string, char)` (I just like to type `std::` and hate `using namespace std;`). Pretty self explanatory with the error message. – crashmstr Dec 15 '14 at 19:33
  • Well... i get that from one explode function: std::vector explode(std::string const & s, char delim) { std::vector result; std::istringstream iss(s); for (std::string token; std::getline(iss, token, delim); ) { result.push_back(std::move(token)); } return result; } – Nicox Dec 15 '14 at 19:36
  • There is no `explode` shown in your code, and the error references `getline`, which unless there is a non `std` version of `getline` you have, that is the problem. And the problem is not complicated (assuming you mean `string` instead of `char` for 2nd portion). This is simple string parsing. – crashmstr Dec 15 '14 at 19:41
  • I don't know, i'm using Xcode, i found this function here: [link](http://stackoverflow.com/questions/12966957/is-there-an-equivalent-in-c-of-phps-explode-function) and it works... But i really have problems with converting string to char and string to float from that v[0] v[1] ... etc according this function... im stuck in that from 5 hours :/ – Nicox Dec 15 '14 at 19:46

1 Answers1

1

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;
}
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • Its almost that i want! I was thinking to use this line2 to insert info into class. So i need to save them into different variable. But i think i will do something with if and else :D – Nicox Dec 15 '14 at 20:24
  • @Nicox I don't get you. What tou mean with *insert info into class* and *something with if and else*. by the way, if this help you some how, please don't forget upvopte. – Raydel Miranda Dec 15 '14 at 20:28
  • i have: char const * n[100]; float c; float w; and i should insert them with: s[++sCount].InportStudent(n,c,w); Now i start to make it with if like: cout << line2 << endl; fi++; if (fi == 1) { strcpy( n, line2); } But isn't working. I try and n = line2; too but still isn't working :/ – Nicox Dec 15 '14 at 20:34
  • @Nicox why don't you use string for `n`? – Raydel Miranda Dec 15 '14 at 20:43
  • because i can't use return n; if its string :( – Nicox Dec 15 '14 at 20:49
  • thie thing is you have declared n as constant and you have to initialize it at the moment you declare it. Or otherwise, remove the constant modifier. – Raydel Miranda Dec 15 '14 at 20:52
  • Actually i don't know what i did before, but now i change it again to string, and it works :O Thanks for the help! :) – Nicox Dec 15 '14 at 20:55