• C

urgent: mkdir in c program

hi there,

i am trying to make subdirectories using either C/C++ . is it possible.

I read that i needed to use #include<direct.h> and then use, mkdir("nameofdir"); but i got errors.

any help is appreciated. tq
zizi21Asked:
Who is Participating?
 
evilrixSenior Software Engineer (Avast)Commented:
>> i read somewhere not to use system
I wouldn't use system to do this -- it's a bit of a fudge. You are relying on an external command being available and you are repeatedly starting a new process for each call making it slow.

You *can* use the posix mkdir function to do this.
http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html

You just need to make the top-level folders first as it doesn't support creating folders recursively. The alternative is to use the Boost create_directories function which, IIRC, does.
http://live.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/reference.html#create_directories
0
 
evilrixSenior Software Engineer (Avast)Commented:
Are you trying to create subfolders recursively? If so you can't use mkdir to do that directly, you'll need to write code to do the recursion.

Can you post your code and examples of the paths you're trying to create?
0
 
sarabandeCommented:
you could use system call like

system("mkdir /dsk1/any/foo/newfolder");

Open in new window


Sara
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
zizi21Author Commented:
all i have is mkdir("nameofdir")

what i am trying to do is, have a main directory called main which i can do in unix mkdir main.

then, in hte main directory, have many many directories. these directories are sub directories to main.

i read somewhere not to use system as it makes the system vulnerable. is this true ? appreciate your reply.
0
 
ssnkumarCommented:
The files to be included in your code are:
#include <sys/stat.h>
#include <sys/types.h>

And the command syntax is:
int mkdir(const char *pathname, mode_t mode);
0
 
ssnkumarCommented:
For example:

int status;
status = mkdir("nameofdir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

The 2nd argument is the mode and that tells what permissions are needed for this directory.
0
 
ssnkumarCommented:
In the above example, the permissions that are given are:
read, write and execute permissions for owner and group
and read and execute permissions for others.
In case of directories, the execute permission means, permission to browse the directory.
0
 
sarabandeCommented:
you need the full path to the main folder or it will not work.

if an attacker has hijacked your system and was able to update the executable file or the loaded program in memory, the attacker could try to locate the system call in your executable and manipulate the string passed to system such that your program would do a different command. there are a lot of if's in the before sentence, and a hijacker once on your system probably can find easier ways to execute shell commands with the current user rights. but principally the system call is more dangerous than mkdir call.

Sara
0
 
zizi21Author Commented:
the error that i am getting is :

test.c: In function ‘main’:
test.c:50: error: expected declaration or statement at end of input

#include <sys/stat.h>
#include <sys/types.h>

int status;
status = mkdir("nameofdir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
0
 
ssnkumarCommented:
So, can you post your complete code here?
0
 
sarabandeCommented:
you need to post all test.c code so that we can see why line 50 has a wrong statement.

Sara
0
 
zizi21Author Commented:
#include<stdlib.h>
#include<stdio.h>
#include<direct.h>
#include<sys/stat.h>
#include<sys/types.h>

int main()
{
int status=mkdir("u",S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

return 0;
}
0
 
zizi21Author Commented:
the error i am getting is error: direct.h: No such file or directory
0
 
ssnkumarCommented:
You don't need:
#include<direct.h>

And I was able to compile this code, without any problem.
Also execute the a.out and the new directory 'u' got created.
0
 
ssnkumarCommented:
> test.c:50: error: expected declaration or statement at end of input

So, what is at line: 50?
Is that solved now?
0
 
zizi21Author Commented:
hi, i want to reward points to sarabande as well as her explaination helped as well. how do i open the questions to reward the marks ?
0
 
ssnkumarCommented:
I don't know, how the question can be reopened.
Better you contact the moderator regarding this.
Or you can look at help topics, which might give some clue.
0
 
zizi21Author Commented:
thanks for your help.
0
 
sarabandeCommented:
thanks.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.