Search code examples
c++system

User defined directory for a c++ system function to execute from


system("mkdir C:\\Users\\USER\\Desktop\\test");

i found this however this does not work because my code looks like this

string inputFAT = "input.FAT";
    string outputdirecotry = "adirectory";
    string exepath = "FATool.exe";

    cout << "enter the directory you would like to have the files put out to";
    getline(cin, outputdirecotry);
    string outputdirectorycommand = "cd " + outputdirecotry;


    cout << "enter the path of the file you want to extract";
    getline(cin, inputFAT);

    cout << "enter the path to the FATool executable";
    getline(cin, exepath);

    string  exportcommand = exepath + " -x " + inputFAT;
    system(outputdirectorycommand.c_str && exportcommand.c_str());

as you can see i need the user to define the directory that the system function needs to go to and when i try to build it it throws these errors

Severity Code Description Project File Line Suppression State Error C3867 'std::basic_string,std::allocator>::c_str': non-standard syntax; use '&' to create a pointer to member FATool++ c:\users\russ\documents\visual studio 2015\projects\fatool++\fatool++\main.cpp 24

and also this

Severity Code Description Project File Line Suppression State Error C2664 'int system(const char *)': cannot convert argument 1 from 'bool' to 'const char *' FATool++ c:\users\russ\documents\visual studio 2015\projects\fatool++\fatool++\main.cpp 24

so is it even possible to do this or should i just take my loses and define the directory myself and have my friends go into the code and do the same thing


Solution

  • The parameter passed to system() is wrong:

    system(outputdirectorycommand.c_str && exportcommand.c_str());
    

    The syntax outputdirectorycommand.c_str is wrong, and the parameter passed to system() is bool which is obviously wrong.

    Assume what you want to do is to execute cd <x> && FATool.exe -x <xxx>, then you should cat your command and pass it to system():

    string cmdToExecute = outputdirectorycommand + " && " + exportcommand;
    system(cmdToExecute.c_str());