473,403 Members | 2,270 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,403 software developers and data experts.

fopen() question.

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

Nov 14 '05 #1
10 1907
Longfellow <no*@this.address> writes:
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
--
Dag-Erling Smørgrav - de*@des.no
Nov 14 '05 #2

In article <86************@xps.des.no>, de*@des.no (=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=) writes:
Longfellow <no*@this.address> writes:
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.
If it can. fopen() may fail.
Checked the FAQ and did not see this question addressed.

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 mi************@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_
Nov 14 '05 #3
On 2005-05-04, Michael Wojcik <mw*****@newsguy.com> wrote:

<snip>
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

Nov 14 '05 #4

In article <11*************@corp.supernews.com>, Longfellow <no*@this.address> writes:

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 mi************@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
Nov 14 '05 #5
mw*****@newsguy.com (Michael Wojcik) writes:
"Dag-Erling Smørgrav" <de*@des.no> writes:
Does everything have to be in the FAQ?

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
--
Dag-Erling Smørgrav - de*@des.no
Nov 14 '05 #6
Dag-Erling Smørgrav wrote:

mw*****@newsguy.com (Michael Wojcik) writes:
"Dag-Erling Smørgrav" <de*@des.no> writes:
Does everything have to be in the FAQ? 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.


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

Nov 14 '05 #7
Kenneth Brody <ke******@spamcop.net> writes:
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.)

--
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 #8
On 2005-05-05, Keith Thompson <ks***@mib.org> wrote:
Kenneth Brody <ke******@spamcop.net> writes:
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

Nov 14 '05 #9
Keith Thompson wrote:

Kenneth Brody <ke******@spamcop.net> writes:
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:Th*************@gmail.com>
Nov 14 '05 #10

In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.org> writes:
Kenneth Brody <ke******@spamcop.net> writes:

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


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 mi************@microfocus.com
Nov 14 '05 #11

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

Similar topics

5
by: Shaoyong Wang | last post by:
Dear All, I want to write a simple PHP code to verify whether a given list of URLs is broken or not. The URLs given have various formats, for example, http://www.afro.com/history/history.html...
4
by: Robert Schott | last post by:
Hi .. this is for sure the 1k question on fopen but maybe can someone tell me this weird staff if i'm writing: $H = fopen($value,"r"); while(!feof($H)) { $string = fgets($H,1024); .... .......
3
by: Ming | last post by:
When I use fopen on the URL: fopen("http://home.pchome.com.tw/world/qoo4ko/110.jpg";, "r") I would get the following error message: -----------Error Message------------ Warning: fopen():...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
10
by: Grocery Clerk | last post by:
I know open() returns a file descriptor and fopen() returns a pointer to FILE. The question is, when do I use fopen() and when do I use open()? Could someone give me an example when to use one...
6
by: rfhurley | last post by:
I'm a newbie at this... I'm trying to run a PHP script from the W3C PHP tutorial, and the example shows the following code: <html> <body> <?php $file=fopen("welcome.txt","r"); ?>
10
by: Julia | last post by:
Hi, there, I am trying to append a binary file by using: FILE *strean; stream = fopen( "c:\temp.dat", "ba+" )); Is there a way that I can check if the file exists or not before fopen,...
25
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { if (argc != 2) { printf("Usage: <program-name<text-file>\n");
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.