Hello. I'm trying to write a program that includes saving text to a file. I wish to have the filename entered by the user. I know how to write to a file, but I'm not sure how to allow the user to define that file. This is the function I tried creating, and when added, my program will not compile:
Code:
void filewrite() {
string filen = "";
cout<<"Enter filename(will be overwitten if exists):";
cin>>filen;
ofstream myfile;
myfile.open (filen, ios::trunc);
myfile << "Writing this to a file.\n";
myfile.close();
}
I appreciate any assistance you can render me.
Edit:
Here are my includes:
Code:
#include <iostream>
#include <sstream>
#include <fstream>
Here is the error:
cpp:15: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >:pen(std::string&, const std::_Ios_Openmode&)’
/usr/include/c++/4.3/fstream:626: note: candidates are: void std::basic_ofstream<_CharT, _Traits>:pen(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]