make Directory argv[]

Jun 10, 2008 at 3:19pm
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,

Jun 10, 2008 at 6:06pm
sw=="-h"
sw=="-help"
sw=="-o"
doesn't work, he compares to pointers
use
if (strcmp(sw, "-h") == 0)
istead
Jun 10, 2008 at 7:48pm
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.
Jun 10, 2008 at 10:17pm
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.
Jun 10, 2008 at 11:08pm
I'll second that.
Jun 11, 2008 at 6:12am
@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.. ?

Jun 11, 2008 at 6:13am
@goti..,

it works super also he compares two strings...

ciao,
Jun 11, 2008 at 7:09am
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 Jun 11, 2008 at 7:10am
Jun 11, 2008 at 1:04pm
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.