C++删除和重命名命令 我使用Android的IDE和CPPIDLE来用C++来编码。我在使用文件重命名和删除命令时遇到一些问题。我猜你不能这么做。我有用户输入,不知道他们会输入什么,所以我需要一个变量或字符串来输入文件的路径名 remove("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt"); rename("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/temp.txt","/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt");

C++删除和重命名命令 我使用Android的IDE和CPPIDLE来用C++来编码。我在使用文件重命名和删除命令时遇到一些问题。我猜你不能这么做。我有用户输入,不知道他们会输入什么,所以我需要一个变量或字符串来输入文件的路径名 remove("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt"); rename("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/temp.txt","/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt");,android,c++,Android,C++,是我使用的代码,它有一个错误。是否有其他方法可以编写此文件,以便添加output2来定位文件 错误是: NDK:无法将参数“1”的“std::basic_string”转换为“char const*”,将其转换为“int removechar const*” NDK:无法将参数“1”的“std::basic_string”转换为“char const*”,将其转换为“int removechar const*” 您的函数需要一个字符数组,但您正在向它传递一个字符串对象。您需要在字符串对象上调用.

是我使用的代码,它有一个错误。是否有其他方法可以编写此文件,以便添加output2来定位文件

错误是:

NDK:无法将参数“1”的“std::basic_string”转换为“char const*”,将其转换为“int removechar const*”

NDK:无法将参数“1”的“std::basic_string”转换为“char const*”,将其转换为“int removechar const*”

您的函数需要一个字符数组,但您正在向它传递一个字符串对象。您需要在字符串对象上调用.c_str

因此/storage/simulated/0/MyGame/MyHackGame/jni/gameFiles/+output2+.txt本身需要是一个字符数组

实现这一点的一种方法是将其存储为字符串,然后将c_str传递到函数中

std::string loc{"/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt"};

remove(loc.c_str());

好的,这里是没有错误的工作原理

std::string loc ("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt");
remove(loc.c_str());
std::string loc1("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/temp.txt");
std::string loc2("/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt");
rename(loc1.c_str() , loc2.c_str());

这将崩溃或更糟。是因为我将一个c字符串附加到一个字符串文本并刚刚捕获它,还是我遗漏了什么?将const char*添加在一起从来都不是一个好主意。在问题的评论中看到我的重复链接,看看应该怎么做。基本上,如果你添加一些,它将是正确的。我想就是这样。我把我的答案改为匹配@NathanOliver