c ++删除并重命名命令

时间:2015-07-01 17:12:52

标签: android c++

我正在使用aide和cppdroid for android来编写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");

我使用的代码是否有错误。有没有其他方法可以写这个,所以我可以添加output2来找到文件?

错误是:

  

NDK:无法将'std :: basic_string'转换为'char const *'以将参数'1'转换为'int remove(char const *)'

2 个答案:

答案 0 :(得分:0)

  

NDK:无法转换&#st; :: basic_string'到#char char *'争论' 1' to' int remove(char const *)'

你的功能是期待一个字符数组,但是你要传递一个字符串对象。您需要在字符串对象上调用.c_str()

std::basic_string::c_str()

所以"/storage/emulated/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());

答案 1 :(得分:0)

好的,这是没有错误的。

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());
相关问题