getline();

Sep 8, 2013 at 5:29am
i am confused with syntax
which one is correct



1
2
3
4
5
ifstream fileptr;
string line;
fileptr.getline(line,100);
or
getline(fileptr,line)
Sep 8, 2013 at 5:42am
In this case line 5.

istream::getline() is used when you store data in a char array http://www.cplusplus.com/reference/istream/istream/getline/
When you have an std::string you need to use a global std::getline() http://www.cplusplus.com/reference/string/string/getline/
Sep 8, 2013 at 5:45am
closed account (S6k9GNh0)
Assuming you've correctly initialized the ifstream and you've opened a file, the second one would be most correct.

The istream class provides a getline member function as well but it only accepts char *: http://www.cplusplus.com/reference/istream/istream/getline/

What you're looking for is http://www.cplusplus.com/reference/string/string/getline/ which is a part of the string header. To be quite honest, I'm ignorant of what the standard says is the difference between the two as far as result and consequence goes.
Last edited on Sep 8, 2013 at 5:47am
Sep 8, 2013 at 5:46am
means there are two getline() function in two different libraries..?
Sep 8, 2013 at 6:03am
thank you!
got it
Topic archived. No new replies allowed.