I am trying to get an input file to open after a user inputs the filename. But whenever I run my test to see if the file is open, it always returns the file did not open error.
I see nothing wrong with your code for the open file statement.
If the input open fails it could mean: the file does not exist, the file is in the wrong directory, or the name is misspelled.
I have found that for most of my work the files I need for input or output need to be in the same directory as the .cpp files. Find out where message.txt is located and you will know where to put the input file.
How do I create the input file without knowing what the user input would be? My output seems to create the file fine, but no text file appears for the input.
The programs runs correctly if I pre-create a txt file in the same directory with the same name as what I input, but fails the open file test if I dont pre-create anything.
Is there a way to get the program itself to create the input file in the correct directory with the given name?
Take my comment with a grain of salt, as I'm learning too.
From what I can tell, ifstream will not create a new file for you. The file already needs to exist on your computer. As long as the file exists, ifstream can open and read from it. http://www.cplusplus.com/reference/fstream/ifstream/open/
Fortunately, you can create a file using ofstream (which you already do for the output file).
If you want to create a file according to the user's input, consider using ofstream to make it and then close it. Afterwards, open the file with ifstream. The file is going to be empty because you never wrote to it, but it will exist.
Here's an example that fails to open the file at first, then creates it and opens it:
How do I create the input file without knowing what the user input would be?
The input file stores information that can be used in the program. User input would normally go into the output file. If you need to read user input from a file first create the file as output, store the information there, close the file, then open it for input.
My output seems to create the file fine, but no text file appears for the input.
inputFile.open(fileName.c_str()); will not create a new file it will only open it. Opening a file for output will create a file if it does not exist.