thought my syntax was sound using ofstream

This code just prints some stuff to a file.

This is what i get when i try to compile in bash g++ terminal


read.cpp: In function ‘int main()’:
read.cpp:13: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.3/fstream:572: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/fstream:556: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/iosfwd:89: note: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)









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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() 
{
string filename;

cout << "enter the filename: ";
getline(cin, filename);

ofstream file_out(filename);
if (! file_out)
{
cout << "File " << filename;
cout << " could not be opened.";
return -1;
}

cout << "File " << filename << " was opened.";
file_out << "Fuck you bitch, " << endl;
file_out << "the cosmic computer." << endl;
file_out << "Fear me.";
file_out.close();
return 0;
}
Try this: ofstream file_out(filename.c_str());
what will that do, why does it do it... and ... a lot more info please lol...

i also got this code out a book... that is teaching me c++
its kinda good/bad


i read it without a computer and taught myself with a real notepad
now im reading it again and its code seems messed up

is c++ without fear
reliable for learning c++
Last edited on
still doesn't work... i tried that...
Sorry, was about to leave work and just threw that line of code out there. The first problem is you were not opening your file. It needs to be opened before you can perform any input/ouput operations. Second, the open() member function takes a c string. So what I'm doing with filename.c_str() is converting your string to a c string. If you were not prompting for the filename you could simply put file_out.open( "filename.txt" );. Make sense? Also see:

http://www.cplusplus.com/reference/iostream/fstream/open/
http://www.cplusplus.com/reference/string/string/c_str/

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() 
{
string filename;

cout << "enter the filename: ";
getline(cin, filename);

ofstream file_out;
file_out.open( filename.c_str() );
if (! file_out)
{
cout << "File " << filename;
cout << " could not be opened.";
return -1;
}

cout << "File " << filename << " was opened.";
file_out << "F#$@ you b#($*, " << endl;
file_out << "the cosmic computer." << endl;
file_out << "Fear me.";
file_out.close();
return 0;
}
Correct me if I'm wrong, but supplying the name of a file to the constructor of any type of fstream will open the specified file, therefore the above method shouldn't make any difference.

Changing line 13 to ofstream file_out(filename.c_str()); compiles fine for me.
Last edited on
You're correct. It doesn't make any difference, it just happens to be the way I wrote the example.
thank you for replies; you've helped alot.
Topic archived. No new replies allowed.