?
Solved

urgent: mkdir in c program

Posted on 2011-04-19
21
Medium Priority
?
575 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:zizi21
  • 8
  • 6
  • 4
  • +1
20 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 35422967
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
 
LVL 35

Expert Comment

by:sarabande
ID: 35423313
you could use system call like

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

Open in new window


Sara
0
 

Author Comment

by:zizi21
ID: 35423476
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Accepted Solution

by:
evilrix earned 1200 total points
ID: 35423613
>> 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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35423618
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35423630
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35423641
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 35423658
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
 

Author Comment

by:zizi21
ID: 35424049
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35424072
So, can you post your complete code here?
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35424080
you need to post all test.c code so that we can see why line 50 has a wrong statement.

Sara
0
 

Author Comment

by:zizi21
ID: 35424087
#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
 

Author Comment

by:zizi21
ID: 35424112
the error i am getting is error: direct.h: No such file or directory
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 400 total points
ID: 35424138
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35424148
> test.c:50: error: expected declaration or statement at end of input

So, what is at line: 50?
Is that solved now?
0
 

Author Comment

by:zizi21
ID: 35424242
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 35424259
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
 

Author Comment

by:zizi21
ID: 35424665
thanks for your help.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35431527
thanks.

Sara
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question