-3

I want to make a simple program that will allow the user to create/open files and add text to them. This is the code I have currently:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    cout << "Enter file name:" << endl;
    char fileName;
    cin >> fileName;
    ofstream myFile;
    myFile.open(fileName, ios::out);
    myFile << "This is the file text.\n";
    myFile.close();
    return 0;
}

I receive the following error at myFile.open(fileName, ios::out):

error: no matching function for call to 'std::basic_ofstream<char>::open(std::__cxx11::string&, const openmode&)'

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

2 Answers2

2

The simple problem you are having here is that the variable filename that stores the name of the file is of type char. Change it to string so that it works.

On another note, try to break down the error message you got:

no matching function for call to 'std::basic_ofstream::open(std::__cxx11::string&, const openmode&)'

In open(std::__cxx11::string& ... it clearly says that the data type of the file name should be string&. This indicated that you had a data type error, which is true because you have used char instead of string.

Another thing: use char only when you want to accept a single letter as input; when you want to take a word or a sentence, store it in a string variable, and get it from the user using the getline() function. This will make your life easier.


To modify your code, firstly change your variable declaration statement to:

    string fileName; // std:: is not required as you have the line "using namespace std"

Secondly, change the input statement of the file name from cin >> filename; to:

    getline(cin, fileName);

It should work after these changes.


EDIT: I found the peoblem to your question. You will nave to change the open command to:

myFile.open(fileName.c_str(), ios::out);

Like it says in the error, the function needs a string passed to ot, however, when we take the string as input and store it in the variable fileName, it simply converts the string into a const char *. This is invisible to you when you run the code, but every once in a while, it causes an error.

This should definitely work now.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • I've made these changes and still am getting an error where I try to open the file ("myFile.open(fileName, ios:: out);). I've tried replacing fileName with "wfldfl" an this ran properly. – Adam Tamargo Feb 10 '17 at 00:12
  • ***and still am getting an error where*** No one can help unless you specify the exact text of the error message. – drescherjm Feb 10 '17 at 00:48
  • Agree with @drescherjm. Specify what the error message says, and which line the error message came from. That may help us in solving your problem. – BusyProgrammer Feb 10 '17 at 01:14
  • My bad. error: no matching function for call to 'std::basic_ofstream::open(std::__cxx11::string&, const openmode&)' – Adam Tamargo Feb 10 '17 at 13:31
  • this occurs at myFile.open(fileName, ios::out); – Adam Tamargo Feb 10 '17 at 13:31
1

If you take a look at the error message, the first half of what is in the open parentheses tells you the answer. The user is typing in a char, the file name is expected to be a string. Instead of:

char fileName;

Use:

string fileName;
gasoline
  • 74
  • 1
  • 7