Error when trying to open file created by user input

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.

Does anyone see why the file would fail to open?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>

using namespace std;

int main()
{
    /*Data Abstraction*/

    string fileName;
    string choice;
    string message;
    ofstream outputFile;
    ifstream inputFile;

    /*Input*/

    //ask for a file name
    cout << "Enter File Name: ";
    cin >> fileName;
    cout << fileName << endl;

    //Choose to encrypt or decrypt
    cout << "Enter encrypt or decrypt: ";
    cin >> choice;
    cout << choice << endl;

    outputFile.open("message.txt");
    inputFile.open(fileName.c_str());
    
    //If encrypt or decrypt was not entered, return error & exit
    if (inputFile.fail())
    {
        cout << "Error: File did NOT open." << endl;
        exit (EXIT_FAILURE);
    }
    if (choice != "encrypt" && choice != "decrypt")
    {
        cout << "Error: Bad Command." << endl;
    }
Hello marc4120,

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.

Hope that helps,

Andy
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string filename;
	cout << "enter file name: ";
	cin >> filename;
	
	// try to open file
	ifstream file_in;
	file_in.open(filename);
	
	// expect this to fail
	if (file_in.is_open())
		cout << "is open" << endl;
	else
		cout << "not open" << endl;

	// create file using filename via ofstream
	ofstream file_out;
	file_out.open(filename);
	if (file_out.is_open())
		cout << "file_out created file" << endl;
	else
		cout << "file_out did not create file" << endl;

	// close it
	file_out.close();

	// reopen it
	file_in.open(filename);
	if (file_in.is_open())
		cout << "file is now open via file_in " << filename << endl;
	else
		cout << "could not open file still" << endl;

	return 0;
}
Last edited on
Hello marc4120,

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.

Hope that helps,

Andy

Topic archived. No new replies allowed.