system () function does not accept string variable in c ++

1

In order to automate some backups that need to be done routinely, then I thought of using an app in dos to do, however I'm having problems compiling the executable because it seems to me that the function system() only accepts char variables, the code I used was as follows.

#include <iostream>
#include <cstdlib>

using namespace std;
int main() {
string usuarios[5] = {"usuario","usuario","usuario","usuario","usuario"};
string diretorios[5];
string path[28];
for (int i = 0; i < 5; ++i) {
    diretorios[i] = "mkdir c:\backups\"+usuarios[i];
    system(diretorios[i]);
    path[i] = "c:\unisystem\backup\"+usuarios[i];
    system("exp system/abcd owner="+usuarios[i]+" file="+path[i]+"\"+usuarios[i]+".dmp log="+path[i]+"\"+usuarios[i]+".log compress=n");
}
cout << "O processo de backup terminou!"<< endl;
system("pause");
return 0;
}

Is there another function that I can replace with system() or do I have to convert string to char?

    
asked by anonymous 10.05.2017 / 20:34

1 answer

1

The system function is not a function originating from C ++ but from the C language, so it gets const char* and not std::string . The C ++ string is nothing more than the C character array encapsulated in a class, and has a method that returns a pointer to that array: c_str()

std::string texto = "Alguma coisa";
texto += " outra coisa";
std::system(texto.c_str());
    
10.05.2017 / 22:44