Reputation: 69
In my code I am trying to create a file and write to it:
std::ofstream saveFile("file.txt");
ofstream << "test" << endl;
Works perfectly! But,
std::string fileName = "file.txt"
std::ofstream saveFile(filename.c_str());
ofstream << "test" << endl;
I've tried with and without c_str(), with no luck. There are no errors. But ofstream.good() returns false.
Upvotes: 0
Views: 380
Reputation: 457
It should work. But do you really post the right code?
At least your code has several obvious syntax errors:
std::string fileName = "file.txt"; // no semicolon
std::ofstream saveFile(fileName.c_str()); // 'fileName' rather than 'filename'
saveFile << "test" << endl; // don't redirect "test" to std::ofstream
// std::ofstream is a class rather than an instance
Please refer to fstream for more detailed information.
Upvotes: 1