See the constructors of std::ofstream
:
basic_ofstream()
Create a data stream from write to file that does not point to any file.
explicit basic_ofstream(const char*, std::ios_base::openmode = ios_base::out)
Creates a write-to-file data flow that points to the file whose name is stored in the array of characters received as the first parameter.
explicit basic_ofstream(const std::filesystem::path::value_type*, std::ios_base::openmode = ios_base::out)
Create a data stream from write to file that points to the file that is in the route received as the first parameter.
explicit basic_ofstream( const std::string&, std::ios_base::openmode = ios_base::out)
Creates a data stream from write to file that points to the file whose name is stored in the string received as the first parameter.
explicit basic_ofstream( const std::filesystem::path&, std::ios_base::openmode = ios_base::out)
Create a data stream from write to file that points to the file that is in the route received as the first parameter.
So you can open a file with std::string
, so your function would look like this:
void guardar_fic(std::string nom_fic){
if (std::ofstream entrada{nom_fic + ".txt"}) {
entrada << "EOO\n";
}
// No es necesario llamar a entrada.close,
// se llama automáticamente al salir del if.
}