简体   繁体   English

不存在从“std::string”到“const char *”的合适转换函数

[英]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::stringc_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.

相关问题 不存在从标准字符串到const char *的合适转换函数 - No suitable conversion function from std string to const char * exists c++ 不存在从“std::string”到“const char*”的合适转换函数 - c++ no suitable conversion function from "std::string" to "const char*" exists 如何修复“没有合适的转换函数从”String“到”const char *“存在”? - How to fix “no suitable conversion function from ”String“ to ”const char *“ exists”? 不存在从“ std :: string”到“ char”的适当转换 - no suitable conversion from “std::string” to “char” exists 从“ const std :: string”到“ time_t”的转换函数不存在 - No suitable conversion function from “const std::string” to “time_t” exists IntelliSense:不存在从“ std :: string”到“ char”的合适转换函数,那么我该如何处理? - IntelliSense: no suitable conversion function from “std::string” to “char” exists so how do i go about this? 从“ std :: string”到“ PVOID”的转换功能不存在 - no suitable conversion function from “std::string” to “PVOID” exists 6 IntelliSense:不存在从“ std :: string”到“ int”的合适转换函数 - 6 IntelliSense: no suitable conversion function from “std::string” to “int” exists 从const char *转换为const std :: string& - Conversion from const char * to const std::string & 我收到错误消息:std :: basic_istream没有合适的转换函数 <char, std::char_traits<char> &gt;到char存在 - I'm getting the error: no suitable conversion function from std::basic_istream<char, std::char_traits<char>> to char exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM