2014-02-26 72 views
-2

我试图在一些基于命令行参数的C++代码中创建一个目录。mkdir在C++系统调用中失败 - int类型的参数与类型为const的参数不兼容char *

命令行参数是一个数字(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())); 

我传递中的参数显然是一个字符串,所以我有点困惑。我是否获得了命令行参数?不知道..

在此先感谢。 詹娜

+2

您是否阅读过['mkdir'手册页](http://man7.org/linux/man-pages/man2/mkdir.2.html)?你知道这个功能实际上做了什么吗? ['system'](http://man7.org/linux/man-pages/man3/system.3.html)函数的作用是什么?提示:错误是* not * from'mkdir'函数调用。 –

回答

4

你并不需要调用system

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

这里mkdir是一个系统调用,而不是字符串(shell命令)传递给system

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

+0

谢谢!我原来是这样的,并且出现错误。我切换到系统并添加了sys头文件。我想这修正了原来的错误。非常感谢你。 – jcam77

相关问题