Hi! I'm new to C++ and am having a problem with saving a text file into a string. I'm using getchar() but only the last line of the rather long text file is saved into the string when used in different functions. In other words, if I include a "cout" that prints the string in main, after the functions, only the last sentence of the text file will be printed.
Would be truly grateful if someone could give me a push in the right direction. The following functions are featured in main() in the same order as the following two function definitions:
string fileName(string &file_name)
{
cout << "Name of file:\n";
cin >> file_name;
int temporary=file_name.rfind(".txt");
if (temporary == string::npos)
file_name.append(".txt");
return file_name;
}
void readFile(string &line, string &file_name)
{
ifstream fin(file_name.c_str() );
if(!fin)
{
cout << "No file with that name available " << file_name << endl;
exit(EXIT_FAILURE);
}
while (!fin.eof())
{
while (getline(fin, line))
{
}
}
}
Best,
John