mkdir在c ++系统调用中失败 - int类型的参数与const char类型的参数不兼容*

时间:2014-02-26 15:33:38

标签: c++ mkdir

我正在尝试根据命令行参数在某些c ++代码中创建一个目录。

命令行参数是一个数字(1,2,3等),我想创建一个文件夹“Output1”,“Output2”等。程序的每个调用只获得1个数字并使1文件夹中。

主要是我:

string folder = "0";
if (argc == 2)
    folder = argv[1];
RunSimulation(c, 0, folder);

然后:

void RunSimulation(Combo c, int it, string folder){
    //Create a directory for the files based on the command line args
    stringstream fileName;
    fileName << "/work/jcamer7/HallSim/Output" << folder;
    system(mkdir(fileName.str().c_str()));

我收到此错误:

error: argument of type "int" is incompatible with parameter of type "const char *" system(mkdir(fileName.str().c_str()));

我传入的参数显然是一个字符串,所以我有点困惑。我是否正确使用命令行arg?不确定..

提前致谢。 珍娜

1 个答案:

答案 0 :(得分:4)

您无需致电system

int mode = 0222; // for example
mkdir(fileName.str().c_str(), mode); // Return-code check omitted for brevity

此处mkdir是系统调用,而不是传递给system的字符串(shell命令)。

可以在mkdir (2)system(3)手册页中找到更多信息。