没有合适的从"std :: string"到"const char*"的转换函数

use*_*887 7 c++ casting

我试图删除.txt文件但文件名存储在类型的变量中std::string.问题是,程序事先不知道文件的名称,所以我不能只使用remove("filename.txt");

string fileName2 = "loInt" + fileNumber + ".txt";
Run Code Online (Sandbox Code Playgroud)

基本上我想做的是:

remove(fileName2);
Run Code Online (Sandbox Code Playgroud)

但是,它告诉我,我不能使用它,因为它给了我错误:

没有合适的从"std :: string"到"const char*"的转换函数.

pax*_*blo 16

remove(fileName2.c_str());
Run Code Online (Sandbox Code Playgroud)

会做的.

a的c_str()成员函数std::string为您提供了const char *可以使用的C风格的字符串版本.