By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

how to create sub-directory if it doesn't exists?

P: n/a
Hi,

I guess in every beginners C-book it writes about how to declare a
filepointer fp and how to create a file (at least in mine)...... Like here:
FILE * fp;
fp = fopen("output_file.txt", "w");

if( fp == NULL)
{
printf("Error: File could not be opened\n");
return 1;
}

....
....

But now I would like to test if a given directory exists and if not, I
want to create it so I can put data-files in that sub-directory... How
to do that? And why is that I guess many text-books don't discuss how to
do this?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 12 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Martin Jørgensen" writes:

I guess in every beginners C-book it writes about how to declare a
filepointer fp and how to create a file (at least in mine)...... Like
here: <snip>
But now I would like to test if a given directory exists and if not, I
want to create it so I can put data-files in that sub-directory... How to
do that? And why is that I guess many text-books don't discuss how to do
this?


The base language has no provisions for directories. But each compiler has
some stuff extracted from the API for the associated OS in header files that
will make sense *after* you know which headers they are :-( . So post your
question on a group that discusses your platform.
Apr 12 '06 #2

P: n/a

Martin Jørgensen wrote:
Hi,

I guess in every beginners C-book it writes about how to declare a
filepointer fp and how to create a file (at least in mine)...... Like here:
FILE * fp;
fp = fopen("output_file.txt", "w");

if( fp == NULL)
{
printf("Error: File could not be opened\n");
return 1;
}

...
...

But now I would like to test if a given directory exists and if not, I
want to create it so I can put data-files in that sub-directory... How
to do that? And why is that I guess many text-books don't discuss how to
do this?


You can't do it portably, so a textbook on standard C wouldn't discuss
it much. K&R discusses some directory stuff in its chapter on Unix.
Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html

-David

Apr 12 '06 #3

P: n/a
On Wed, 12 Apr 2006 15:49:06 +0200,
Martin Jørgensen <un*********@spam.jay.net> wrote
in Msg. <j3************@news.tdc.dk>
But now I would like to test if a given directory exists and if not, I
want to create it so I can put data-files in that sub-directory... How
to do that? And why is that I guess many text-books don't discuss how to
do this?


Because Standard C doesn't know what a "directory" is. FWIW, neither
does this newsgroup.

robert
Apr 12 '06 #4

P: n/a
On 2006-04-12, Martin Jørgensen <un*********@spam.jay.net> wrote:
Hi,

I guess in every beginners C-book it writes about how to declare a
filepointer fp and how to create a file (at least in mine)...... Like here:
FILE * fp;
fp = fopen("output_file.txt", "w");

if( fp == NULL)
{
printf("Error: File could not be opened\n");
return 1;
}

...
...

But now I would like to test if a given directory exists and if not, I
want to create it so I can put data-files in that sub-directory... How
to do that? And why is that I guess many text-books don't discuss how to
do this?
Best regards / Med venlig hilsen
Martin Jørgensen


It would depend on the compiler environment. In Gnu C you could
look up mkdir().
Apr 12 '06 #5

P: n/a
David Resnick wrote:
Martin Jørgensen wrote: -snip-
You can't do it portably, so a textbook on standard C wouldn't discuss
it much. K&R discusses some directory stuff in its chapter on Unix.
Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html


Thanks a lot. That probably tells how to create the directory. And how
to see if the directory exists in the first place?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 12 '06 #6

P: n/a
Martin Jørgensen opined:
David Resnick wrote:
Martin Jørgensen wrote:

-snip-
You can't do it portably, so a textbook on standard C wouldn't
discuss
it much. K&R discusses some directory stuff in its chapter on Unix.
Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html


Thanks a lot. That probably tells how to create the directory. And
how to see if the directory exists in the first place?


You have to ask in a group that discusses your OS/implementation. As
was mentioned elsethread, Standard C knows nothing about directories.

--
"I once witnessed a long-winded, month-long flamewar over the use of
mice vs. trackballs...It was very silly."
(By Matt Welsh)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 12 '06 #7

P: n/a

Martin Jørgensen wrote:
David Resnick wrote:
Martin Jørgensen wrote:

-snip-
You can't do it portably, so a textbook on standard C wouldn't discuss
it much. K&R discusses some directory stuff in its chapter on Unix.
Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html


Thanks a lot. That probably tells how to create the directory. And how
to see if the directory exists in the first place?

So as someone else said, this discussion doesn't really belong
in comp.lang.c. You may get some ideas here:

http://c-faq.com/osdep/fileexists.html

Reading through the FAQ is quite educational, I recommend trying it
as you find time, there is much to learn there.

If you want details on how to do directory manipulation, asking in a
group
dedicated to your particular platform is the way to go. For example,
if you us *nix, asking in comp.unix.programmer is a good idea.

-David

Apr 12 '06 #8

P: n/a
Martin Jørgensen wrote:
David Resnick wrote:
You can't do it portably, so a textbook on standard C wouldn't discuss
it much. K&R discusses some directory stuff in its chapter on Unix.
Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html


Thanks a lot. That probably tells how to create the directory. And how
to see if the directory exists in the first place?


The first link I followed in the page linked to by David Resnick led me
to a new page, within the 'c-faq' site, with enough information to
learn how to check if a directory exists.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 12 '06 #9

P: n/a
Pedro Graca opined:
Martin Jørgensen wrote:
David Resnick wrote:
You can't do it portably, so a textbook on standard C wouldn't
discuss
it much. K&R discusses some directory stuff in its chapter on
Unix. Anyway, see the FAQ:

http://c-faq.com/osdep/mkdir.html


Thanks a lot. That probably tells how to create the directory. And
how to see if the directory exists in the first place?


The first link I followed in the page linked to by David Resnick led
me to a new page, within the 'c-faq' site, with enough information to
learn how to check if a directory exists.


Oh, but that's too difficult!

--
"Whip me. Beat me. Make me maintain AIX."
(By Stephan Zielinski)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.