fopen modes

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Is there a real difference in r+ and w+ ? I read in my man pages that r+
opened a file for reading and writing while w+ opened a file for reading and
writing and if the file didn't exist it would by created. If it existed it
would be truncated.

Bill
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill said:
Is there a real difference in r+ and w+ ? I read in my man pages that r+
opened a file for reading and writing while w+ opened a file for reading and
writing and if the file didn't exist it would by created. If it existed it
would be truncated.

That's the difference. r+ doesn't truncate the file, it just opens it
and allows to read and write. fopen with w+ always returns a handle to
empty file with read and write privileges.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkPXncACgkQPFW+cUiIHNr6iACfZ0wkbBTvQQKyw/8D4kPzDARp
wjMAoIxZbrkMTAY487oUXsqHp9dEwbpz
=iUvy
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

Is there a real difference in r+ and w+ ? I read in my man pages that
r+
opened a file for reading and writing while w+ opened a file for reading
and writing and if the file didn't exist it would by created. If it
existed it would be truncated.

Bill, ask yourself this...
"What would fopen("MyFile","r+") do/return if the file "MyFile" didn't
exist?

Now, ask yourself
"What would fopen("MyFile","w+") do/return if the file "MyFile" didn't
exist?

Are your two answers the same? If not, why not?

(Hint: Does the documentation say that "r+" will create the file if it
doesn't exist?)



--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Bill Cunningham

(Hint: Does the documentation say that "r+" will create the file if it
doesn't exist?)

Too my understanding of the docs that I read on my posix system, no the
file needs to already exist.

Bill
 
L

Lew Pitcher

Too my understanding of the docs that I read on my posix system, no
the
file needs to already exist.

OK, so you've got one difference:
With "r+", the file needs to exist for the fopen() to succeed, but
with "w+", the file does not need to exist.

And, from another post, you learned another difference:
With "r+", the newly opened file will not be truncated, but with "w+", the
file will be truncated.

So, I think that you've answered your own question of if "there a real
difference in r+ and w+".

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill said:
Too my understanding of the docs that I read on my posix system, no the
file needs to already exist.

Was there anywhere said that r+ creates file if it does not exist?
If you are not sure, you should read docs more carefully and/or just
write an example program that will show you what is the meaning of r+,
w+, etc. This is not the case that implementations are different than
specification.

Pawel Dziepak

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkPY4IACgkQPFW+cUiIHNpRFQCgpGU23oMfF3bKEzb8P0mmoqpN
/MAAnifDbXcnJzDGS3EOEQNACQias8ku
=Mtwi
-----END PGP SIGNATURE-----
 
J

John Bode

    Is there a real difference in r+ and w+ ? I read in my man pages that r+
opened a file for reading and writing while w+ opened a file for reading and
writing and if the file didn't exist it would by created. If it existed it
would be truncated.

Bill

r+ allows you to read and modify data already in the file, as well as
adding and modifying new data.

w+ discards data already in the file, and allows you to add and modify
new data.

a+ does not allow you to read or modify data already in the file, but
does allow you to add and modify new data.

If I had file that served as a database of addresses and phone
numbers, and I wanted to be able to read and edit existing records as
well as add new records, I would use r+. If I wanted to throw away
all existing records and start over with all new records, I would use w
+. If I wanted to add new records and keep the existing records
without changing them, I would use a+.
 
B

Barry Schwarz

Too my understanding of the docs that I read on my posix system, no the
file needs to already exist.

If you want a posix answer, ask in a posix group. If you want a C
answer, refer to C documentation since you already have K&R II.
 
B

Barry Schwarz

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



That's the difference. r+ doesn't truncate the file, it just opens it
and allows to read and write. fopen with w+ always returns a handle to

Standard C does not have file handles, or any other handles. If fopen
succeeds, it will return a FILE*. But not always. Even with w+,
fopen can fail and will return NULL.
 
R

ruckuus

Too my understanding of the docs that I read on my posix system, no the
file needs to already exist.

Bill

what posix is your system ?
r+ will return 0 if the file is not exist taken it will return FILE*
w+ will always return FILE*, if file exist it will be truncated to 0

just simply write the code to demonstrate it. you can see it better.
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


According to Wikipedia [1] and other sources: struct FILE* is a file
handler.

[1] http://en.wikipedia.org/wiki/File_descriptor

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQWJsACgkQPFW+cUiIHNqWAwCfeT9N3SowvbP7FQTTNEjJGpHg
SQkAn1EBmiPUK3LLM8WE+WbZPJJp5epx
=lzKc
-----END PGP SIGNATURE-----
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nick Keighley worte:
ITYM "handle". Despite what wikipedia says this is not standard C
terminology

Of course, "handle".
7.19.5.3/8:
"The fopen function returns a pointer to the object controlling the stream."
C99 doesn't says that fopen returns a handle. However, there is no "file
handle" definition in the standard, so why don't call that "object
controlling the stream" a handle? I know that there is no clear
definition what a file handle is, but that doesn't mean that structure
FILE mustn't be called "a handle".

Pawel Dziepak

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQXkUACgkQPFW+cUiIHNr81QCfYlAOfvMsQzFMAQ/9V6C4Aq3a
ZJAAoJFxGFzRUCCe6SGoWtJbESWDOH3L
=WmWa
-----END PGP SIGNATURE-----
 
C

Chris Dollin

Pawel said:
"The fopen function returns a pointer to the object controlling the stream."
C99 doesn't says that fopen returns a handle. However, there is no "file
handle" definition in the standard, so why don't call that "object
controlling the stream" a handle? I know that there is no clear
definition what a file handle is, but that doesn't mean that structure
FILE mustn't be called "a handle".

Since /other/ things are called "file handles", and "a FILE*" is a way
to refer to a FILE* value, then calling a FILE* value a "file handle"
induces more confusion than is perhaps necessary.

Perhaps we should go to the other extreme and call it a "pedal";
I suspect I'll not be allowed to sustain that.

(Do we know that FILE is a struct at all? Clearly it's /likely/, even
/sensible/, to be a struct, but a hasty scan of a draft to hand didn't
actually commit.)

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris said:
Since /other/ things are called "file handles", and "a FILE*" is a way
to refer to a FILE* value, then calling a FILE* value a "file handle"
induces more confusion than is perhaps necessary.

Ok, I see your point and, now, I agree. I think that will end that part
of discussion.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQZsMACgkQPFW+cUiIHNrBdQCfZMTIPP+CL/X0z0xsGxNIYVNy
oNMAnj/zxaFVC0NVHU8bItbWwcwttMqN
=VsgU
-----END PGP SIGNATURE-----
 
N

Nick Keighley

(Do we know that FILE is a struct at all? Clearly it's /likely/, even
 /sensible/, to be a struct, but a hasty scan of a draft to hand didn't
 actually commit.)

I tried arguing once that FILE didn't have to be a struct. But one
of the Powers (I *think* Chris Torek) pointed out that it would be
difficult (I paraphrase) for it not to be a struct. He pointed
out that some of the low level calls (eg. getc) are required to be
implemented as both a macro and a function and (if I recall correctly)
the wording of The Standard almost forced it to be a struct.
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nick said:
I tried arguing once that FILE didn't have to be a struct. But one
of the Powers (I *think* Chris Torek) pointed out that it would be
difficult (I paraphrase) for it not to be a struct. He pointed
out that some of the low level calls (eg. getc) are required to be
implemented as both a macro and a function and (if I recall correctly)
the wording of The Standard almost forced it to be a struct.

7.19.1/2:
"(...) FILE which is an object type capable of recording all the
information needed to control a stream, (...)"
That doesn't *directly* say that FILE is a structure, but I don't think
that there is any other implementation, that's not just proof of concept.

Pawel Dziepak

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQcCYACgkQPFW+cUiIHNob6gCfRY9zoJSam4mTEbfjlpIwl0rv
pyQAn17Cs/Bk6QQ5fvLIGsh7vf63aKQo
=xItn
-----END PGP SIGNATURE-----
 
C

Chris Dollin

Pawel said:
7.19.1/2:
"(...) FILE which is an object type capable of recording all the
information needed to control a stream, (...)"
That doesn't *directly* say that FILE is a structure, but I don't think
that there is any other implementation, that's not just proof of concept.

There's a difference between "it would be daft for FILE not to be a struct"
and "the standard requires FILE to be a struct".

A proof-of-concept implementation would be sufficient as a counter-example
to "FILE is a struct", even if we all agreed that such an implementation
would be sillier than frying eggs with a sunbeam.

(Planets, now, /those/ are the right kind of target.)

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
P

Pawel Dziepak

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Eric said:
Using "handle" to describe what fopen() returns is particularly
bad, because "handle" has already been assigned a meaning -- worse,
it has been assigned a meaning that is related to, but different from,
"pointer to a FILE object." Using "handle" is therefore worse than
using "kibblezy" because it invites, even encourages, confusion.

I don't think that I really see that difference between handle and
object that controls the stream and stores all needed information.

(I know that, I confused FILE with pointer to FILE but in my next posts
I corrected myself.)

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQcdwACgkQPFW+cUiIHNrtUgCeLR91PVPXOf841TLWcZwggYj8
jZgAnR8qY3hY3gQhKb3K+CytN5uI9v09
=wmEQ
-----END PGP SIGNATURE-----
 
J

jameskuyper

Chris Dollin wrote:
....
There's a difference between "it would be daft for FILE not to be a struct"
and "the standard requires FILE to be a struct".

A proof-of-concept implementation would be sufficient as a counter-example
to "FILE is a struct", even if we all agreed that such an implementation
would be sillier than frying eggs with a sunbeam.

I've seen people do that (with a reflector). When you've got enough
sunlight to make it work, there's nothing silly about it - it saves
fuel and doesn't pollute the environment. That makes it poor measure
of silliness.
 

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

Similar Threads

fopen 20
fopen() questions 17
fopen 17
Problem with fopen 13
Working with files 1
HELP:function at c returning (null) 4
Python client/server that reads HTML body from server 1
URGENT 1

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top