I’m working on a project that requires that input must be taken from a file and then processed. We were given a skeleton of the code for opening a file and checking to make sure it is open, but I’m having issues opening the file.
My Code
int main()
{
readFile();
}
void readFile()
{
ifstream fin;
string fileName;
int x;
cout << "Enter the file you wish to read from: ";
cin >> fileName;
fin.open(fileName.c_str(), ios::in);
if(!fin.is_open())
{
cerr << "Unable to open file " << fileName << endl;
exit(10);
}
}
My console:
https://i.stack.imgur.com/2derI.jpg
We were basically given all of this code so I have absolutely no idea why this isn’t working out. This is just a part of my much larger project but I can’t even attempt that part yet until I get this working. Any help would be much appreciated.
Probably your directory settings don’t show the files extension. You need to enter
12.txt.txtinstead of12.txt.