[英]No suitable conversion function from “std::string” to “const char *” exists
I am trying to delete a .txt
file but the filename is stored in a variable of type std::string
.我正在尝试删除
.txt
文件,但文件名存储在std::string
类型的变量中。 The thing is, the program does not know the name of the file beforehand so I can't just use remove("filename.txt");
问题是,程序事先不知道文件的名称,所以我不能只使用
remove("filename.txt");
string fileName2 = "loInt" + fileNumber + ".txt";
Basically what I want to do is:基本上我想做的是:
remove(fileName2);
However, it tells me that I cannot use this because it gives the me error:但是,它告诉我我不能使用它,因为它给了我错误:
No suitable conversion function from "std::string" to "const char *" exists.
不存在从“std::string”到“const char *”的合适转换函数。
remove(fileName2.c_str());
will do the trick.会做的伎俩。
The c_str()
member function of a std::string
gives you the const char *
C-style version of the string that you can use. std::string
的c_str()
成员函数为您提供了可以使用的字符串的const char *
C 样式版本。
You need to change it to:您需要将其更改为:
remove(fileName2.c_str());
c_str()
will return the string as a type const char *
. c_str()
将字符串作为const char *
类型返回。
When you need to convert std::string
to const char*
you can use the c_str()
method.当您需要将
std::string
转换为const char*
您可以使用c_str()
方法。
std::string s = "filename";
remove(s.c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.