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