473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to create folder at fopen?

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
Nov 14 '05 #1
15 46385
tschi-p <ts*****@yahoo.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
<Je***********@physik.fu-berlin.de> wrote in message
news:2p************@uni-berlin.de...
tschi-p <ts*****@yahoo.com> wrote:
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,


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
Nov 14 '05 #3

"tschi-p" <ts*****@yahoo.com> wrote

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.
Nov 14 '05 #4
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Nov 14 '05 #5
tschi-p wrote:
[ . . . ] 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[i] != '\0'; i++) {
if (i>0 && (mname[i] == '\\' || mname[i] == '/')) {
char slash = mname[i];
mname[i] = '\0';
if (mkdir(mname) errors && error isn't ALREADY_EXISTS) {
return NULL;
}
mname[i] = slash;
}
}
free(mname);
return fopen(name, mode);
}

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #6
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
"Derrick Coetzee" <dc****@moonflare.com> wrote in message
news:ch**********@news-int.gatech.edu...
tschi-p wrote:
[ . . . ] 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[i] != '\0'; i++) {
if (i>0 && (mname[i] == '\\' || mname[i] == '/'))
{
char slash = mname[i];
mname[i] = '\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[i] = slash;
}
}
free(mname);
return fopen(name, mode);
}


Please do not post nonstandard code here.

-Mike
Nov 14 '05 #8
Mike Wahler wrote:
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.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #9
Derrick Coetzee wrote:
Mike Wahler wrote:
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.


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.

--
Er*********@sun.com

Nov 14 '05 #10

"Derrick Coetzee" <dc****@moonflare.com> wrote

Mike Wahler wrote:
Please do not post nonstandard code here.


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.
Nov 14 '05 #11
hm, I didn't know creating folders is not supported by C.
anyway, thank you for your answers
"tschi-p" <ts*****@yahoo.com> wrote in message
news:2p************@uni-berlin.de...
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

Nov 14 '05 #12
"tschi-p" <ts*****@yahoo.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13

In article <ch**********@news7.svr.pol.co.uk>, "Malcolm" <ma*****@55bank.freeserve.co.uk> writes:

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 mi************@microfocus.com

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
Nov 14 '05 #14
Michael Wojcik <mw*****@newsguy.com> scribbled the following:
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.


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #15

In article <ch**********@oravannahka.helsinki.fi>, Joona I Palaste <pa*****@cc.helsinki.fi> writes:

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 "\/".

--
Michael Wojcik mi************@microfocus.com
Nov 14 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave | last post by:
I have an application where I am providing the user the ability to select or create a folder on a domain, using SHBrowseForFolder. When the user selects/creates a folder on a remote computer, it...
2
by: DCount17 | last post by:
I'm new to .NET and C#. I need help in trying to do the following: I need to use a URI (or something else) that allows all users of the application to be able to access a directory and create a...
1
by: sajithm | last post by:
Hi all, I have a problem while trying to create folder and files in a remot storage server using asp.net. Actually I was trying with filesystem object and this says bad usernam and passeord,....
0
by: Lars Netzel | last post by:
I'm creating a setup project for my application so that users will have a proper installation. In the installation I need to copy a database over to the clients D:\ partition and create a folder...
1
by: deepaks85 | last post by:
Dear Sir, I have written a php script to create folder : <?php $targetpath = $_SERVER . "/reports/"; $target = $targetpath . basename( $_FILES); mkdir($targetpath.$vName,0700); ?>
0
by: mrinalaryan | last post by:
create folder & upload file in the web server through web service-C# -------------------------------------------------------------------------------- im using web service to connect windows form...
2
by: shona | last post by:
Hello friends.., I want to create folder on server from client machine using Asp.net... My problem is that i have done coding it run properly but whenever i ran that application from IIS...
1
by: jags | last post by:
HI All I was trying to create Document librray programatically in a Sharepoint Site and is not working for me.I was trying to implement this as an Event Handler. The aim of the code is to create...
2
by: Noorain | last post by:
sir how to create folder to use php script? i know how to file upload in folder to use php. but i can't how to create folder. i type name & its create my folder. Example: Type: test. my...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.