Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Simple (well not to me) C++ question about mkdir..
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
richjoyce
n00b
n00b


Joined: 22 Oct 2002
Posts: 20
Location: Canada

PostPosted: Sun Oct 12, 2003 9:27 pm    Post subject: Simple (well not to me) C++ question about mkdir.. Reply with quote

I have this C++ file I want to compile, and it has a line like this:

mkdir(nom);

(nom is a variable), but when i compile it errors with:
foo.cpp:280: `mkdir' undeclared (first use this function)
foo.cpp:280: (Each undeclared identifier is reported only once for
each function it appears in.)


Any ideas on how to fix this?
Back to top
View user's profile Send private message
MADcow
l33t
l33t


Joined: 23 Jan 2003
Posts: 742
Location: RIT (Henrietta, New York, United States)

PostPosted: Sun Oct 12, 2003 9:37 pm    Post subject: Reply with quote

how about including stdlib and using system(command)
Back to top
View user's profile Send private message
MADcow
l33t
l33t


Joined: 23 Jan 2003
Posts: 742
Location: RIT (Henrietta, New York, United States)

PostPosted: Sun Oct 12, 2003 9:38 pm    Post subject: Reply with quote

oh yea, and mkdir and such usually resides in unistd.h (standard unix commands, like sleep and stuff)... you might try including that.
Back to top
View user's profile Send private message
richjoyce
n00b
n00b


Joined: 22 Oct 2002
Posts: 20
Location: Canada

PostPosted: Mon Oct 13, 2003 12:55 am    Post subject: Reply with quote

Ok I changed it to system("mkdir nom"); and it compiles fine, but when i run it it doesn't recognize nom as a variable, it just makes the dir 'nom'...how do i pass variables to system()?
Back to top
View user's profile Send private message
MADcow
l33t
l33t


Joined: 23 Jan 2003
Posts: 742
Location: RIT (Henrietta, New York, United States)

PostPosted: Mon Oct 13, 2003 2:03 am    Post subject: Reply with quote

it would take too long to explain, so why not look here?
http://www-tcsn.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20740454.html
Back to top
View user's profile Send private message
Target
Apprentice
Apprentice


Joined: 25 Apr 2002
Posts: 200

PostPosted: Mon Oct 13, 2003 5:29 am    Post subject: Reply with quote

Gah! Don't look there. They don't bounds-check.
sprintf() is evil. Use snprintf() instead.

You have to dynamically create a c-type string that contains the full command, and pass that to system(). You can do this by printing into the string to get your variable contents written out properly.

Here:
Code:
#include <limits.h> /* for NAME_MAX */

char cmd[NAME_MAX + 7]; /* "mkdir " + null == 7 */
snprintf(cmd, NAME_MAX + 6, "mkdir %s", nom);
system(cmd);

Now, if you're absolutely, completely, positively sure that your variables can never match or exceed the allocated size of the destination string...
...Use snprintf() anyway because you may have missed something.

Or if you're already using STL strings anyway (#include <string>):
Code:
string cmd = "mkdir " + non;
system(cmd.c_str());

Alternatively, you could just use the mkdir function by including the correct header:
Code:
#include <sys/stat.h> /* for mkdir() */

if (mkdir(nom, 0755)) { perror(nom); exit(1); }
Back to top
View user's profile Send private message
MADcow
l33t
l33t


Joined: 23 Jan 2003
Posts: 742
Location: RIT (Henrietta, New York, United States)

PostPosted: Mon Oct 13, 2003 1:54 pm    Post subject: Reply with quote

Target is right.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum