I'm trying to get a better grasp on pointers. My class assignment was to create the function for the prototype void OpenFile(const char *fileName, ifstream &inFile)
.
void OpenFile(const char *fileName, ifstream &inFile)
{
inFile.open(FILENAME, ios_base::in);
if (!inFile.is_open()) {
cerr << "Could not open file " << fileName << "\n";
exit(EXIT_FAILURE);
}
else {
cout << "File Open successful";
}
}
//FILENAME is defined as: const char * const FILENAME = "file.txt";
// function is called in main with: OpenFile(FILENAME, inFile);
I guess what I don't understand is the const char * and the & for the two arguments. I am guessing that the first argument is a const char * because that's how the assignment defined FILENAME. But did I have to use a pointer in this case? Could I have just done
const char FILENAME = "file.txt";
and in my OpenFile to have the first parameter just take in a const char. And then again, why do I need a reference for the second parameter of my function? Definitely confused on when to use pointers and when to use references. Thanks!