Question #80831

Pass in an ifstream (must be a reference argument [what type of parameter is it?]). The
function prompts the user and, if the file is found, it returns true and the ifstream will be a
handle for that text file. Otherwise the function returns false, and the argument is assumed to
be undefined. How does that look code-wise?

Expert's answer

#include <iostream>
#include <fstream>
#include <cstdlib>

char filename[20] = "experiment.txt";
ifstream inpFile;
inpFile.open(filename);
if (!inpFile.is_open())
{
cout << " Could not open the file " << filename << endl;
exit (EXIT_FAILURE);
}
LATEST TUTORIALS
APPROVED BY CLIENTS