How to create folder at fopen?

J

Jens.Toerring

tschi-p said:
maybe my question is quite simple, but although searching like wild I cannot
find a solution.
I just want to append to a file, let's say to "c:\my folder\filename1.txt".

Be careful with the backspaces, you may need more than you show here;-)
Normally with append-option, the file is created if it doesn't exist. But I
want also the folder to be created if it does not exist - now if the
specified folder is not available, the file isn't created at all.
Is there an additional option at fopen that I couldn't find or are there
other useful functions that do exactly this?

No, there isn't. That's because the C standard doesn't even admit
that folders exist. So everything to do with folders (directories
for those from the UNIX camp;-) has to be done using system specific
functions. Perhaps your system has some functions (or even extensions
to fopen())) that does what you are looking for, but about this you
have to ask in a group that deals with your system.

Regards, Jens
 
M

Mike Wahler

folder\filename1.txt".

Be careful with the backspaces,

That made me blink for a minute. :)

I'm sure you meant to say 'backslashes'.

"c:\\my folder\\filename1.txt"

However, Windows (which I'll assume OP is using, from his
mention of 'folder'), will also accept forward slash as
path separator:

"c:/my folder/filename1.txt".

-Mike
 
M

Malcolm

tschi-p said:
Normally with append-option, the file is created if it doesn't exist. But I
want also the folder to be created if it does not exist - now if the
specified folder is not available, the file isn't created at all.

Is there an additional option at fopen that I couldn't find or are there
other useful functions that do exactly this?
This is entirely up to the OS. Some may create a folder when passed as part
of a path to fopen(), most do not. You need to use platform-specific
functions to use the directory services. This is irritating, but that's what
the standard says.
 
D

Derrick Coetzee

tschi-p said:
[ . . . ] I want also the folder to be created if it does not exist [ . . . ]

As others have said, there's no standard support for this. It isn't hard
to write though - the following (completely unverified) code should do
the trick, assuming your platform-specific make directory function is
called "mkdir" and you've filled in the appropriate error stuff:

/* Opens the file, creating directories in its pathspec if necessary */
FILE* fopen_mkdir(const char* name, const char* mode) {
char* mname = strdup(name);
int i;
for(i=0; mname != '\0'; i++) {
if (i>0 && (mname == '\\' || mname == '/')) {
char slash = mname;
mname = '\0';
if (mkdir(mname) errors && error isn't ALREADY_EXISTS) {
return NULL;
}
mname = slash;
}
}
free(mname);
return fopen(name, mode);
}
 
S

SM Ryan

# want also the folder to be created if it does not exist - now if the
# specified folder is not available, the file isn't created at all.

Whether stdio can do anything with directories except handle file
paths to files with directory name, this is system dependent. In
general you have to handle directories by constructing commands
to pass to system() or with implementation dependent calls.
 
K

Keith Thompson

Malcolm said:
This is entirely up to the OS. Some may create a folder when passed as part
of a path to fopen(), most do not. You need to use platform-specific
functions to use the directory services. This is irritating, but that's what
the standard says.

I'm not aware of *any* systems on which fopen() will implicitly create
a folder/directory/whatever. (Which doesn't imply that there aren't
any, of course.)
 
T

tschi-p

maybe my question is quite simple, but although searching like wild I cannot
find a solution.
I just want to append to a file, let's say to "c:\my folder\filename1.txt".

Normally with append-option, the file is created if it doesn't exist. But I
want also the folder to be created if it does not exist - now if the
specified folder is not available, the file isn't created at all.

Is there an additional option at fopen that I couldn't find or are there
other useful functions that do exactly this?

tschi-p
 
M

Mike Wahler

Derrick Coetzee said:
tschi-p said:
[ . . . ] I want also the folder to be created if it does not exist [ .
.. . ]

As others have said, there's no standard support for this. It isn't hard
to write though - the following (completely unverified) code should do
the trick, assuming your platform-specific make directory function is
called "mkdir" and you've filled in the appropriate error stuff:

/* Opens the file, creating directories in its pathspec if necessary */
FILE* fopen_mkdir(const char* name, const char* mode) {
char* mname = strdup(name);

Please cite where the C standard defines 'strdup()'.
int i;
for(i=0; mname != '\0'; i++) {
if (i>0 && (mname == '\\' || mname == '/'))
{
char slash = mname;
mname = '\0';
if (mkdir(mname) errors && error isn't ALREADY_EXISTS) {


Please cite where the C standard defines 'mkdir()'.
Please cite where the C standard defines an operator 'isn't'.
Please cite where the C standard defines 'ALREADY_EXISTS'.
return NULL;
}
mname = slash;
}
}
free(mname);
return fopen(name, mode);
}


Please do not post nonstandard code here.

-Mike
 
D

Derrick Coetzee

Mike said:
Please do not post nonstandard code here.

The strdup() was an error on my part, but I explicitly indicated that
the rest was pseudocode to be filled in for a particular environment.
Standard C may not have such facilities, but it does have facilities for
calling functions. Would you feel better if I called the hypothetical
make directory function "foo" or "bar"? I've seen enough posts call
those and they certainly aren't in the standard library.

In short, I think a little platform-agnostic code to help solve
someone's C problem shouldn't be disallowed.
 
E

Eric Sosman

Derrick said:
The strdup() was an error on my part, but I explicitly indicated that
the rest was pseudocode to be filled in for a particular environment.
Standard C may not have such facilities, but it does have facilities for
calling functions. Would you feel better if I called the hypothetical
make directory function "foo" or "bar"? I've seen enough posts call
those and they certainly aren't in the standard library.

In short, I think a little platform-agnostic code to help solve
someone's C problem shouldn't be disallowed.

It's not quite as platform-agnostic as you may think.
For example, the backslash character \ is a perfectly legal
(albeit seldom used) file name character on Unix, and does
not divide one directory from the next; your code would botch
it badly. You might also find it a sobering experience to
contemplate OpenVMS paths like

$disk1:[fdr1.fdr.fdr3]file.ext;7

One oddity of this scheme is that the names of the various
directories look like

$disk1:[]fdr1.dir;1
$disk1:[fdr1]fdr2.dir;1
$disk1:[fdr1.fdr2]fdr3.dir;1

.... meaning that you can't form the proper name for "mkdir"
simply by truncating the original path, nor even by shuffling
bits and pieces: you actually need to supply new characters
that didn't exist in the original. One can even come up with
a file name for which the directory name alone is longer than
the complete original name, e.g.

$disk2:[folder]x.c;3
$disk2:[]folder.dir;1

And it doesn't end there; I've barely scratched the heavily-
encrusted surface of OpenVMS file specifications, inspired as
they were by both the Rococo and the Art Deco movements. The
full overelaborated messiness might drive you to apoplexy --
it very nearly did so to me, back in the days when I was trying
to get path-bashing code similar in spirit to yours ported onto
a VMS system.
 
M

Malcolm

Derrick Coetzee said:
The strdup() was an error on my part,
In short, I think a little platform-agnostic code to help solve
someone's C problem shouldn't be disallowed.
Mike Wahler was seeing red because "mkdir" happens to be a common extension,
and is of course off-topic. However you were within the bounds of
topicality, becuase you said it was just a placeholder for a
platform-specific directory make function.
Unfortunately there is also no standard for path composition. For some
reason probably to do with legal rights, the two most common operating
systems use different separators.
 
T

tschi-p

hm, I didn't know creating folders is not supported by C.
anyway, thank you for your answers
 
K

Keith Thompson

tschi-p said:
hm, I didn't know creating folders is not supported by C.
anyway, thank you for your answers

Please don't top-post.

To be precise, creating folders is not supported by *standard* C. Any
C implementation on a system that allows folders or directories to be
created is nearly certain to provide a library routine that will
create a folder or directory. ("mkdir" is one common name for such a
routine.)

This means that any C program that creates folders is non-portable,
but there's no great sin in writing non-portable C if that's what you
need to do. This just isn't the place to discuss non-portable
features.
 
M

Michael Wojcik

Unfortunately there is also no standard for path composition. For some
reason probably to do with legal rights, the two most common operating
systems use different separators.

MS-DOS 1.0 used the slash character to delimit program options; it did
not require a space between the program name and an option. (This
convention was adopted from some other OSes of the era; more details
can be found via Google, as this has been hashed out before.)

Thus when MS-DOS 2.0 introduced hierarchical directories to the FAT
filesystem, it had to use a character other than the slash to
separate path components, because otherwise

program/option

and

directory/program

would be ambiguous. Microsoft chose the backslash (aka "reverse
virgule", etc) for that purpose, and subsequent MS OSes have
inherited it.

--
Michael Wojcik (e-mail address removed)

Company Secretary Finance Manager
1) Half-grey haired executives.
2) Must be waist-deep in their field of activities.
3) Must be having the know-how and the do-how of the latest
developments in their respective fields.
-- from "Appointments Vacant" section of Business India
 
J

Joona I Palaste

Michael Wojcik said:
MS-DOS 1.0 used the slash character to delimit program options; it did
not require a space between the program name and an option. (This
convention was adopted from some other OSes of the era; more details
can be found via Google, as this has been hashed out before.)
Thus when MS-DOS 2.0 introduced hierarchical directories to the FAT
filesystem, it had to use a character other than the slash to
separate path components, because otherwise



would be ambiguous. Microsoft chose the backslash (aka "reverse
virgule", etc) for that purpose, and subsequent MS OSes have
inherited it.

And I think they have subsequently caused people to think that \ is the
preferred slash, and / is the non-preferred backslash, because after
all, their computers are that way. For example, I have already seen
people write URLs with a schema denotation of "http:\\", which is not
valid under any definition of a URL.
My biggest surprise was when I went to the webpage of a Finnish
student accomodation service, and they had a dropdown menu to select
which university or college you are studying in. For Helsinki
university, they had an option for each faculty, consisting of the
name of the university and the name of the faculty, separated by a
"\" character. For example "HY\Matemaattis-luonnontieteellinen" for
the faculty of sciences.
I don't think that in conventional typography, the "\" character has any
defined meaning. I honestly don't remember ever seeing it in a normal
book, document or other printed material. Therefore I think it was
invented specially for computers.
 
M

Michael Wojcik

I don't think that in conventional typography, the "\" character has any
defined meaning. I honestly don't remember ever seeing it in a normal
book, document or other printed material. Therefore I think it was
invented specially for computers.

I don't know whether the backslash has prior use outside of
computing, but its introduction to computing is generally credited to
Bob Bemer, "The Father of ASCII", who died recently. I've read that
he introduced it so that the logical "and" and "or" (disjunction and
conjunction) could be written as "/\" and "\/".
 

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