Hi, I have a question with ofstream, is there anyway to ask the user for a file name, then create the file, and write to it?
Like:

string file;
cout << "Enter filename:";
getline(cin, file);
ofstream ofile;
ofile.open(file);
ofile << (stuff....);
ofile.close;

...except that doesn't work properly.:S
Thanks for any help.

ofile.open(file.c_str());
...
ofile.close(); // function call!

In the current C++ Standard fstream::open member function wants const char* argument only, not const std::string& .

Hi, I have a question with ofstream, is there anyway to ask the user for a file name, then create the file, and write to it?
Like:

string file;
cout << "Enter filename:";
getline(cin, file);
ofstream ofile;
ofile.open(file);
ofile << (stuff....);
ofile.close;

...except that doesn't work properly.:S
Thanks for any help.

Assuming you're using a std::string to hold your filename, I think you just need to convert it to a c style string in your call to ofile.open....
i.e.

ofile.open(file.c_str());

Hope this is of some help,
Jas.

Thanks guys, it works great now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.