fopen() question.

L

Longfellow

Newbie here...

My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

Thanks,

Longfellow
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

Longfellow said:
My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

Does everything have to be in the FAQ? Your C library documentation
should mention this prominently, so there is no need for the FAQ to
mention it. Here's an excerpt from the library documentation on my
system:

``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.

In fact, the first sentence is a almost-direct quote from the Standard
(the word "file" only occurs once in the original).

DES
 
M

Michael Wojcik

If it can. fopen() may fail.

What question?
Does everything have to be in the FAQ?

And what's *your* problem? "I checked the FAQ" is a perfectly
reasonable, indeed polite, remark. There's no call to complain about
it. Let's not discourage people from reading the FAQ before posting.

If the OP would like to explain just what his question is, I'm
sure we'd be glad to help.

--
Michael Wojcik (e-mail address removed)

I said, 'I need to put my soul into my work and it is well known that
computers haven't got a soul.' My father said, 'The Americans are working
on it.' -- Sue Townsend, _The Secret Diary of Adrian Mole, Aged 13 3/4_
 
L

Longfellow

If the OP would like to explain just what his question is, I'm
sure we'd be glad to help.

Thanks.

fopen() in write mode does indeed open a new file if one does not exist.
If one's code is correct, that is... ;) Sorry for the unnecessary post.

Longfellow
 
M

Michael Wojcik

fopen() in write mode does indeed open a new file if one does not exist.
If one's code is correct, that is... ;)

Well, it may not even for correct code. For example (though this is
outside the scope of the C language), fopen may be unable to create a
file because the program does not have sufficient permission. The
standard doesn't say fopen ever has to succeed; in fact, it can always
fail, though that's not a particularly useful implementation.

Also, note that opening a file for update (with "r+") does not create
a file, so if you want to open only an existing file for writing you
can use that mode. What fopen lacks is a mode that says "open this
file, if it exists, for writing (not necessarily appending) but
without discarding the contents; create it if it does not exist". To
do that you need two calls to fopen: one with "r+" and another, if
the first fails, with "w" (possibly followed by closing the file and
opening it again with "r+" if you really do want update mode).

(OT: With two calls there is a potential race condition, and for
security reasons it'd be desirable to have such a mode for implemen-
tations where it could be done atomically; but since that would be
platform-dependant anyway, you may as well write platform-dependant
code if this is a concern.)

--
Michael Wojcik (e-mail address removed)

The antics which have been drawn together in this book are huddled here
for mutual protection like sheep. If they had half a wit apiece each
would bound off in many directions, to unsimplify the target.
-- Walt Kelly
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

And what's *your* problem? "I checked the FAQ" is a perfectly
reasonable, indeed polite, remark.

The FAQ is a FAQ, not a reprint of the standard. It is intended to
address ambiguous, unclear or difficult parts of the C language. The
fact that fopen() tries to create the file if it does not already
exist and the specified mode was "w" or "w+" is neither ambiguous,
unclear nor difficult, and should be cleary stated in any text which
describes fopen(), including compiler documentation, OS manual pages,
K&R2 and other tutorial or reference texts, etc.

DES
 
K

Kenneth Brody

Dag-Erling Smørgrav said:
The FAQ is a FAQ, not a reprint of the standard. It is intended to
address ambiguous, unclear or difficult parts of the C language. The
fact that fopen() tries to create the file if it does not already
exist and the specified mode was "w" or "w+" is neither ambiguous,
unclear nor difficult, and should be cleary stated in any text which
describes fopen(), including compiler documentation, OS manual pages,
K&R2 and other tutorial or reference texts, etc.

Re-quoting the original statement in question:
My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

I don't think he's saying "why isn't it in the FAQ", but rather "I did
the right thing and checked the FAQ, but since I didn't find an answer
there I am posting here".

Considering how many questions asked here are answered with "read the
FAQ, section x.y", I would think we should applaud those people who not
only took the time to check the FAQ to see if their question was answered
there, but realize that that's an important enough step that they even
told us they checked. (Just in case it's there but they missed it.)

Not every question is, nor should be, in the FAQ. But that doesn't mean
that you shouldn't look there first, anyway.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
Dag-Erling Smørgrav wrote: [...]
Re-quoting the original statement in question:
My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

I don't think he's saying "why isn't it in the FAQ", but rather "I did
the right thing and checked the FAQ, but since I didn't find an answer
there I am posting here".

Fair enough, but I think it's perfectly understandable that someone
might interpret it to mean that the OP was surprised that the question
wasn't answered in the FAQ. (I misunderstood it that way myself.)
 
L

Longfellow

Kenneth Brody said:
Dag-Erling Smørgrav wrote: [...]
Re-quoting the original statement in question:
My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

I don't think he's saying "why isn't it in the FAQ", but rather "I did
the right thing and checked the FAQ, but since I didn't find an answer
there I am posting here".

Fair enough, but I think it's perfectly understandable that someone
might interpret it to mean that the OP was surprised that the question
wasn't answered in the FAQ. (I misunderstood it that way myself.)

Ye Hairy Gods!!! I have created something of a micro-mini-monster...;)

Decided I should really read this NG for content and ran across my post,
but with these further responses. And discovered that my original post
was a subtle marvel of ambiguity!

Fact is, I left out the defining penultimate sentence: It should have
been something to the effect that "it wasn't doing what it should in the
code I was writing". After posting, and feeling rather foolish, I went
back to the canonical "gcc -g -Wall -ansi -pedantic..." and got all the
information I needed to discover by mistake. Hence my second post.

Apologies to all who took the time to pay attention to my poorly written
initial post: My Bad!!

Addendum: I do full error checking on all file activities, a la H&S,
even though I'm only writing trivial stuff for my own use.

Thanks again,

Longfellow
 
K

Kenneth Brody

Keith said:
Kenneth Brody said:
Dag-Erling Smørgrav wrote: [...]
Re-quoting the original statement in question:
My reading of the description of fopen() led me to expect that, with
mode as "w", it would create a file if it did not exist. Checked the
FAQ and did not see this question addressed. What don't I understand?

I don't think he's saying "why isn't it in the FAQ", but rather "I did
the right thing and checked the FAQ, but since I didn't find an answer
there I am posting here".

Fair enough, but I think it's perfectly understandable that someone
might interpret it to mean that the OP was surprised that the question
wasn't answered in the FAQ. (I misunderstood it that way myself.)

Which, of course, may be the case.

Maybe I haven't been jaded enough, and I prefer to give the poster the
benefit of the doubt until they demonstrate otherwise. ;-)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Michael Wojcik

Fair enough, but I think it's perfectly understandable that someone
might interpret it to mean that the OP was surprised that the question
wasn't answered in the FAQ. (I misunderstood it that way myself.)

That in no way excuses Dag-Erling Smørgrav's attack. A poster who
asks why something is not covered by the FAQ at least has read the
thing.

Chastising someone for reading the FAQ does the group no good. That's
behavior we want to encourage.
 

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,294
Messages
2,571,518
Members
48,225
Latest member
JasminOrta

Latest Threads

Top