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

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

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
 
O

osmium

:

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

David Resnick

Martin said:
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
 
R

Robert Latest

On Wed, 12 Apr 2006 15:49:06 +0200,
in Msg. said:
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
 
R

Richard G. Riley

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().
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

David said:
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
 
V

Vladimir S. Oka

Martin Jørgensen opined:
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>
 
D

David Resnick

Martin said:
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
 
P

Pedro Graca

Martin said:
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.
 
V

Vladimir S. Oka

Pedro Graca opined:
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!
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top