-2

I am using aide and cppdroid for android to code in c++. I'm having some trouble with the file rename and remove commands. My guess is you can't do this. I have user input and don't know what they will type in so I need a variable or string to input for the path name of the file.

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");

Is the code I used and it has an error. Is there some other way I can write this so I can add the output2 to locate the file?

Error is:

NDK:cannot convert 'std::basic_string' to 'char const*' for argument '1' to 'int remove(char const*)'

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

2 Answers2

0

NDK:cannot convert 'std::basic_string' to 'char const*' for argument '1' to 'int remove(char const*)'

You're function is expecting an array of characters, but you're passing it a string object. You need to call .c_str() on your string object.

std::basic_string::c_str()

So the "/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/" + output2 + ".txt" needs to be an array of characters itself.

One way to achieve this would be to store it as a string, then pass the c_str() into the function.

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

remove(loc.c_str());
Acorn
  • 1,147
  • 12
  • 27
0

Ok, here's what worked with no errors.

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());
Mixcels
  • 889
  • 1
  • 11
  • 23