Connecting Tech Pros Worldwide Forums | Help | Site Map

fopen() question.

Longfellow
Guest
 
Posts: n/a
#1: Nov 14 '05
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


Dag-Erling Smørgrav
Guest
 
Posts: n/a
#2: Nov 14 '05

re: fopen() question.


Longfellow <not@this.address> writes:[color=blue]
> 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?[/color]

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
--
Dag-Erling Smørgrav - des@des.no
Michael Wojcik
Guest
 
Posts: n/a
#3: Nov 14 '05

re: fopen() question.



In article <8664xz2z37.fsf@xps.des.no>, des@des.no (=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=) writes:[color=blue]
> Longfellow <not@this.address> writes:[color=green]
> > 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.[/color][/color]

If it can. fopen() may fail.
[color=blue][color=green]
> > Checked the FAQ and did not see this question addressed.[/color][/color]

What question?
[color=blue]
> Does everything have to be in the FAQ?[/color]

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 michael.wojcik@microfocus.com

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_
Longfellow
Guest
 
Posts: n/a
#4: Nov 14 '05

re: fopen() question.


On 2005-05-04, Michael Wojcik <mwojcik@newsguy.com> wrote:

<snip>[color=blue]
> If the OP would like to explain just what his question is, I'm
> sure we'd be glad to help.[/color]

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

Michael Wojcik
Guest
 
Posts: n/a
#5: Nov 14 '05

re: fopen() question.



In article <117ifg53mvhe84d@corp.supernews.com>, Longfellow <not@this.address> writes:[color=blue]
>
> fopen() in write mode does indeed open a new file if one does not exist.
> If one's code is correct, that is... ;)[/color]

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 michael.wojcik@microfocus.com

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
Dag-Erling Smørgrav
Guest
 
Posts: n/a
#6: Nov 14 '05

re: fopen() question.


mwojcik@newsguy.com (Michael Wojcik) writes:[color=blue]
> "Dag-Erling Smørgrav" <des@des.no> writes:[color=green]
>> Does everything have to be in the FAQ?[/color]
> And what's *your* problem? "I checked the FAQ" is a perfectly
> reasonable, indeed polite, remark.[/color]

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
--
Dag-Erling Smørgrav - des@des.no
Kenneth Brody
Guest
 
Posts: n/a
#7: Nov 14 '05

re: fopen() question.


Dag-Erling Smørgrav wrote:[color=blue]
>
> mwojcik@newsguy.com (Michael Wojcik) writes:[color=green]
> > "Dag-Erling Smørgrav" <des@des.no> writes:[color=darkred]
> >> Does everything have to be in the FAQ?[/color]
> > And what's *your* problem? "I checked the FAQ" is a perfectly
> > reasonable, indeed polite, remark.[/color]
>
> 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.[/color]

Re-quoting the original statement in question:
[color=blue]
> 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?[/color]

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:ThisIsASpamTrap@gmail.com>

Keith Thompson
Guest
 
Posts: n/a
#8: Nov 14 '05

re: fopen() question.


Kenneth Brody <kenbrody@spamcop.net> writes:[color=blue]
> Dag-Erling Smørgrav wrote:[/color]
[...][color=blue]
> Re-quoting the original statement in question:
>[color=green]
>> 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?[/color]
>
> 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".[/color]

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.)

--
Keith Thompson (The_Other_Keith) kst-u@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.
Longfellow
Guest
 
Posts: n/a
#9: Nov 14 '05

re: fopen() question.


On 2005-05-05, Keith Thompson <kst-u@mib.org> wrote:[color=blue]
> Kenneth Brody <kenbrody@spamcop.net> writes:[color=green]
>> Dag-Erling Smørgrav wrote:[/color]
> [...][color=green]
>> Re-quoting the original statement in question:
>>[color=darkred]
>>> 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?[/color]
>>
>> 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".[/color]
>
> 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.)[/color]

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

Kenneth Brody
Guest
 
Posts: n/a
#10: Nov 14 '05

re: fopen() question.


Keith Thompson wrote:[color=blue]
>
> Kenneth Brody <kenbrody@spamcop.net> writes:[color=green]
> > Dag-Erling Smørgrav wrote:[/color]
> [...][color=green]
> > Re-quoting the original statement in question:
> >[color=darkred]
> >> 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?[/color]
> >
> > 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".[/color]
>
> 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.)[/color]

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:ThisIsASpamTrap@gmail.com>


Michael Wojcik
Guest
 
Posts: n/a
#11: Nov 14 '05

re: fopen() question.



In article <lnr7glqo9e.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org> writes:[color=blue]
> Kenneth Brody <kenbrody@spamcop.net> writes:[color=green]
> >
> > 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".[/color]
>
> 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.)[/color]

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.

--
Michael Wojcik michael.wojcik@microfocus.com
Closed Thread


Similar C / C++ bytes