How to create a new directory in C

M

munanxue

I remember that I could create a new folder in the current directory easily
in Unix and Linux by something like
system("mkdir sub_directory");
But failed to do so under Windows System. Any suggestions?
 
C

Christopher Benson-Manica

munanxue said:
I remember that I could create a new folder in the current directory easily
in Unix and Linux by something like
system("mkdir sub_directory");
But failed to do so under Windows System. Any suggestions?

Sure, even though this question is not topical for this group:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

The DOS command to create a directory is md, not mkdir. Passing Unix
commands to a DOS shell is, for obvious reasons, unlikely to
produce the results you want.
 
G

Gordon Burditt

I remember that I could create a new folder in the current directory easily
in Unix and Linux by something like
system("mkdir sub_directory");
But failed to do so under Windows System. Any suggestions?

What command would you type at a command prompt to create a directory
in Windows? system() uses a command interpreter that tends to be
the same as one of the ones used by interactive users.

Also realize that the concepts of "current directory" work a little
differently between Windows and Unix. So do file/directory permissions.

Gordon L. Burditt
 
M

munanxue

1. mkdir can create a new directory in Windows, just as md.
2. system("md sub_directory"); failed as well, i tested before I posted
this thread.
any other suggestions?
 
C

code_wrong

2. system("md sub_directory"); failed as well, i tested before I posted


That's very odd .. it works as expected here on XP home.
does system() work with other shell commands?

code compiled with gcc:

#include <stdio.h>
int main(void)
{
system("md sub_directory");
system("mkdir sub_dir2");
return 0;
}
 
T

TJ

1. mkdir can create a new directory in Windows, just as md. 2.
system("md sub_directory"); failed as well, i tested before I posted
this thread.
any other suggestions?

You should read the bottom part of Gordon's post again:


Before calling the Windows(tm) md command in your code, did you change
the current working directory (in your code)? If not, did you search the
_entire_ drive (probably C:) for the directory you created. The CWD
(current working directory) environment variable is different on Windows
and unless you changed it, your new directory could be almost anywhere ...
but usually ends up in the "My Documents" folder.
 
T

TJ

1. mkdir can create a new directory in Windows, just as md.
2. system("md sub_directory"); failed as well, i tested before I posted
this thread.
any other suggestions?

Oops ... still too trigger happy on the "post message" command ...

Beyond my previous suggestion, please qualify what you mean by
"system("md sub_directory"); failed as well" ... how do you know it
failed? How did it fail? What is the return value of the system() call?
etc ...


Sincerely,
-TJ Walls
Ph.D Candidate - Physics Dept. Stony Brook University
 
M

Mike Wahler

Christopher Benson-Manica said:
Sure, even though this question is not topical for this group:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

The DOS command to create a directory is md, not mkdir.

Either one will work in DOS.

An example of why posting off topic answers is not advised. :)

Passing Unix
commands to a DOS shell is, for obvious reasons, unlikely to
produce the results you want.

Agreed.

-Mike
 
M

munanxue

First, thanks for all of your help.

For the unclarity of "it failed as well", I mean it didn't create a new
directory, as it should.

gcc will compile both system("md sub1") and system("mkdir sub2") as long
as the parameter passed in system() is a string. But both commands failed
to create a new directory.

For those who suggested the current directory problem, I tested system("md
c:\sub_directory") and it didn't work.

For those who think this thread is off topic, I am sorry if I bothered
anybody by posting this question, and this was unintentional. I didn't
consider this is a MS-DOS programming question, since what I am looking
for is just a nice function call that does the job for me.

Best Regards
 
G

Gordon Burditt

For the unclarity of "it failed as well", I mean it didn't create a new
directory, as it should.

Are you sure it didn't create a new directory *ANYWHERE*?
Did you check the entire system for one?
gcc will compile both system("md sub1") and system("mkdir sub2") as long
as the parameter passed in system() is a string. But both commands failed
to create a new directory.

For those who suggested the current directory problem, I tested system("md
c:\sub_directory") and it didn't work.

What is a \s character? I believe using such a character results in the
wrath of undefined behavior. Also, you're probably better off using
an absolute path. Hint: system("md c:\\sub_directory");

Gordon L. Burditt
 
M

Mark McIntyre

1. mkdir can create a new directory in Windows, just as md.
2. system("md sub_directory"); failed as well, i tested before I posted
this thread.
any other suggestions?

Yes, ask in a windows programming group. How the command that you execute
via system works is offtopic here.

<OT>
top guess, you don't have permission to create a file where system() is
spawning its shell. Try system("dir > c:\foo.txt") and see where you
actually /are/ at the time. I bet its not where you expect.
</OT>
 
F

Florian Weingarten

Mark McIntyre said:
spawning its shell. Try system("dir > c:\foo.txt") and see where you

Should be system("dir > c:\\foo.txt"), shouldn't it?


Flo
 
M

Mark McIntyre

Should be system("dir > c:\\foo.txt"), shouldn't it?

The behaviour of the string passed to system is implementation defined, and
offtopic here....
gd&r
 
P

pete

Mark said:
The behaviour of the string passed to system is implementation
defined, and offtopic here....
gd&r

Are you aware that your original string contains a formfeed character?
 
F

Florian Weingarten

Mark McIntyre said:
The behaviour of the string passed to system is implementation defined, and
offtopic here....

Are you sure about that? The C standard says:

Alphabetic escape sequences representing nongraphic characters in the execution
character set are intended to produce actions on display devices as follows:

\f (form feed) Moves the active position to the initial position at the start of the next
logical page.

(ISO/IEC 9899:1999 (E), 5.2.2 Character display semantics)


Flo
 
C

caroundw5h

Wow, does anything ever get done around here?

Code:
#include <stdio.h>
#include <stdlib.h>// for system
int main(void)
{
     system("md HEREIAM");
     system("mkdir PICKABOO");
     return 0;
}

compile the following on windows save it in the C: directory then go
back and look for the file names passed as arguments.
or if you want you can pass the path to system after md:
ex: md path\kdkdk\kdkdk\kkdkd
 
K

Keith Thompson

Florian Weingarten said:
Are you sure about that? The C standard says:

Alphabetic escape sequences representing nongraphic characters in
the execution character set are intended to produce actions on
display devices as follows:

\f (form feed) Moves the active position to the initial position at
the start of the next logical page.

(ISO/IEC 9899:1999 (E), 5.2.2 Character display semantics)

There's no indication that a character in a string passed to system()
is going to be sent to a display device. In this particular case, it
almost certainly won't be. I don't know what would happen on a
Windows system, but the equivalent command on a Unix-like system would
likely create a file whose name contains a formfeed character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top