i have the next code:
std::string line;
std::ifstream myfile ("text.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
std::cout << line << std::endl;
}
myfile.close();
}
is there a way to do it, and use char* instead of string?
So you’re looking for a more “C-like” solution?
…plus some check whether the file was correctly opened. In this case, you use
fgets()on the filef, reading into the char*buffer. However, buffer has onlyENOUGHspace allocated and this limit is also an important parameter to thefgets()function. It will stop reading the line when reachingENOUGH - 1characters, so you should make sure theENOUGHconstant is large enough.But if you didn’t mean to solve this in a “C-like” way, but are still going to use
<iostream>, then you probably just want to know that thec_str()method ofstd::stringreturns thechar*representation of thatstd::string.