Reputation: 58
I am using fstream
to read or write into a file.
I meet a problem regarding the function ifstream::getline
. When I include <string>
, then I can pass string as a parameter to the function, if not, I have to pass char*
as the parameter. Can someone tell me why?
According to the description of the function, as I understand it, the right parameter waited should be char*
. Is the string
value converted to a char*
here?
Upvotes: 0
Views: 805
Reputation: 153830
There are two versions of std::getline()
:
std::istream::getline(char*, char = '\n'); // member function of istream
std::getline(std::istream&, std::string&, char = '\n'); // free function in <string>
When including the definition of std::istream
you obviously get the member function. However, you'll need to include <string>
to get the definition of std::string
which also comes with its version of std::getline()
.
Upvotes: 2
Reputation: 324
String type isn't converted to char*
. That's why .c_str()
exists. But the std::string type will accept a char*
as an initializer.
The two types aren't backwards compatible. std::string
's will accept char*
's but char*
's don't accept std::string
's.
Upvotes: 2