Can I specify what file I want to write into in C++? I want to be able to type in the filename and write into that file. When I try making myfile.open("example.txt") myfile.open(var), I get a big error…
error: no matching function for call to ‘std::basic_ofstream >::open(std::string&)’
/usr/include/c++/4.2.1/fstream:650: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]
Can you make any sense of this or explain what I am doing wrong? I have a feeling this is very simple, as this is my first week using C++.
If
varis an std::string, try:The error tells you exactly what’s wrong, although the precision of the template types named doesn’t help make that crystal clear. Take a look at the reference for .open(). It takes a
const char *for the filename, and another optional mode parameter. What you are passing is not aconst char *.