To make your second verson work, you could add the flag ios_base::out
:
void TestFunction (fstream &test,char FileName []){
cout << "test !!!" << endl;
test.open(FileName, std::ios::out);
// ^^^^^^^^^^^^
test << "test test test" << endl;
test.close();
}
If you just want to write content to a file, you could choose more specific version which is std::ofstream
.
Because basic_ofstream
constructor is designed to take const char* as input parameter, so it doesn't accept
std::string`, however, this is changed in C++11.
explicit basic_ofstream( const char* filename,
ios_base::openmode mode = ios_base::out ); // before C++11
explicit basic_ofstream( const string& filename,
ios_base::openmode mode = ios_base::out ); // (since C++11)