If I change myfile(“input.txt”) to myfile(file_name)…where file_name is passed to the function it does not work but gives the error no matching function call..which I’m guessing b.c. I’m not suppose to be passing a string to the constructor…if not this way..how?
void file_to_string(string file_name)
{
string line;
ifstream myfile("input.txt");
if(myfile.is_open())
{
while(myfile.good())
{
getline(myfile,line);
cout << line;
}
myfile.close();
}
else
{
cout << "File : " << file_name << " : did not open" ;
}
}
int main(int argc, char *argv[])
{
file_to_string(argv[1]);
}
Use the
c_str()member of thestd::stringclass:It returns a null-terminated
const char *representation of the string in question, which is exactly what you need here.