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

mkdir failing in c++ system call - argument of type int incompatible with parameter of type const char*

本文关键字:类型 参数 char 不兼容 const int C++ 系统调用 失败 MKDIR      更新时间:2023-10-16

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

命令行参数是一个数字(1、2、3 等),我想创建一个"输出 1"、"输出 2"等文件夹。 程序的每次调用仅获得 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()));

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

提前谢谢。珍娜

你不需要调用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)手册页中找到。