We help IT Professionals succeed at work.

urgent: mkdir in c program

zizi21
zizi21 asked
on
Medium Priority
615 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
Comment
Watch Question

evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
you could use system call like

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

Open in new window


Sara

Author

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.
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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);

Commented:
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.

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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);

Commented:
So, can you post your complete code here?
CERTIFIED EXPERT
Top Expert 2016

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

Sara

Author

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;
}

Author

Commented:
the error i am getting is error: direct.h: No such file or directory
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
> test.c:50: error: expected declaration or statement at end of input

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

Author

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 ?

Commented:
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.

Author

Commented:
thanks for your help.
CERTIFIED EXPERT
Top Expert 2016

Commented:
thanks.

Sara
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.