Opening file...

X

X-Terror

How do I open a file (with fopen(), or similar conforming
with ANSI C) so I can read/write in file, and if file doesn't
exist to be created?

Thanks!
 
N

nrk

X-Terror said:
How do I open a file (with fopen(), or similar conforming
with ANSI C) so I can read/write in file, and if file doesn't
exist to be created?

Thanks!

Slightly tricky. mode "r+" doesn't work as it doesn't create the file when
it doesnt exist. mode "a+" almost works, but will only let you append to
the file and positions the stream at the end of the file. One solution is
to open the file with mode "a+", close it and re-open with mode "r+".
Would like to see a better solution from a more clueful regular.

-nrk.
 
P

Peter Pichler

X-Terror said:
How do I open a file (with fopen(), or similar conforming
with ANSI C) so I can read/write in file, and if file doesn't
exist to be created?

FILE * f;

f = fopen(filename, "r+");
if (!f)
f = fopen(filename, "w");

....
 
P

Peter Pichler

Peter Pichler said:
FILE * f;

f = fopen(filename, "r+");
if (!f)
f = fopen(filename, "w");

OTOH, maybe not. The first fopen may fail for some other reason other than
the file not existing. If the second fopen succeeds, well... bad luck :(
 
T

those who know me have no need of my name

in comp.lang.c i read:
X-Terror wrote:
One solution is to open the file with mode "a+", close it and re-open with
mode "r+". Would like to see a better solution from a more clueful
regular.

that's about as good as it gets in standard c. use freopen instead of
fclose then fopen, just in case the implementation can optimize it. using
"a" first is an option, which may use slightly fewer resources -- of course
it might use more too.
 
M

Manohar S

Peter Pichler said:
FILE * f;

f = fopen(filename, "r+");
if (!f)
f = fopen(filename, "w");

Regarding the last line.(If the "if" condition succeds).
I am not understanding how come on earth the file that is
opened for write can also be used for read by using "w" access mode.
Could anyone please explain this if I am wrong???
 
P

Peter Pichler

Manohar S said:
"Peter Pichler" <[email protected]> wrote in message

Regarding the last line.(If the "if" condition succeds).
I am not understanding how come on earth the file that is
opened for write can also be used for read by using "w" access mode.
Could anyone please explain this if I am wrong???

That's one of the reasons why I said perhaps not, in a separate thread.
That'll teach me for posting to c.l.c. at 1.30 in the morning ;-)
 
O

Old Wolf

How do I open a file (with fopen(), or similar conforming
OTOH, maybe not. The first fopen may fail for some other reason other than
the file not existing. If the second fopen succeeds, well... bad luck :(

How about:

f = fopen(filename, "a+");
if (!f) /* abort */ ;
fseek(f, 0, SEEK_SET);

a+ Open for reading and writing. The file is created
if it does not exist. The stream is positioned at
the end of the file.
 
N

nrk

Old said:
How about:

f = fopen(filename, "a+");
if (!f) /* abort */ ;
fseek(f, 0, SEEK_SET);

a+ Open for reading and writing. The file is created
if it does not exist. The stream is positioned at
the end of the file.

"a" and "a+" force all writes to the end of the file regardless of fseek
calls. This may be a problem for the OP. Best solution seems to be fopen
with "a", fclose and fopen with "r+" [freopen mode changes might not be
allowed by the implementation].

-nrk.
 

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

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top