正确保存到具有文件名的文件

Properly Saving to file with a file name

本文关键字:文件名 文件 确保 保存      更新时间:2023-10-16
 ofstream myfile;
    string s=r->str_name+".txt";
    myfile.open (s);

其中r->str_name为字符串。如果r->str_name是"animals",如果我像这样连接,它会把文件保存为animals.txt吗?

关闭。它确实如您所期望的那样,r->str_name将是"animals.txt",但要将其传递给myfile.open(),您必须将其转换为const char*,如下所示:

myfile.open (s.c_str());