make Directory argv[]

Hello all,

i want to make a directory from the the input of the User (argv). I have wrote it.. but it didn't work. Can you help me.. ;-) ?

int main (int argc, char * const argv[])

{
for (int commandOption = 1; commandOption < argc ; commandOption++) {
string sw = argv[commandOption];

if (sw=="-h" || sw=="-help") {

usage();
return -1;

}else if (sw=="-o") {
commandOption++;
string OutDir = argv[commandOption];
mkdir(OutDir);
.................

............// the rest of my program.....

}

thanks in advance,

sw=="-h"
sw=="-help"
sw=="-o"
doesn't work, he compares to pointers
use
if (strcmp(sw, "-h") == 0)
istead
sw is a std::string, so his comparison code is fine.

The mkdir() function only takes a char* --it doesn't understand strings, so you have to say:

mkdir( OutDir.c_str() );

Hope this helps.
I'm going to put a plug in for Boost here. The boost library has a really good command line parser called 'program options'. Makes doing this stuff really simple.
I'll second that.
@Duoas

thanks for you, but unfort. "mkdir( OutDir.c_str() );" didn't work. I have recieved this error mesage: " error: too few arguments to function 'int mkdir(const char *, mode_t)' :-(

so do you have another suggestaion.. ?

@goti..,

it works super also he compares two strings...

ciao,
mkdir() has two arguments; the directory name and mode_t is the permissions to be set on that directory when it's created. I'm guessing that you're doing this on UNIX/Linux in which case do
man -s2 mkdir
on the command line for an explanation of how to use it, or google will find man pages as well.
Last edited on
Yeah, I can't remember arguments to anything. When I program I always have a reference so that I can look things up. I just assumed that you had the mkdir() argument list correct. In either case, though, you will still need to use c_str(), since mkdir() doesn't take a std::string.
Topic archived. No new replies allowed.