fopen modes

Discussion in 'C Programming' started by Bill Cunningham, Nov 3, 2008.

  1. 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
    Bill Cunningham, Nov 3, 2008
    #1
    1. Advertising

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

    Bill Cunningham wrote:
    > 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-----
    Pawel Dziepak, Nov 3, 2008
    #2
    1. Advertising

  3. Bill Cunningham

    Lew Pitcher Guest

    On November 3, 2008 15:14, in comp.lang.c, Bill Cunningham
    () wrote:

    > 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. ------
    Lew Pitcher, Nov 3, 2008
    #3
  4. "Lew Pitcher" <> wrote in message
    news:c3f43$490f5f27$4c0aa7c0$...

    > (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
    Bill Cunningham, Nov 3, 2008
    #4
  5. Bill Cunningham

    Lew Pitcher Guest

    On November 3, 2008 15:38, in comp.lang.c, Bill Cunningham
    () wrote:

    >
    > "Lew Pitcher" <> wrote in message
    > news:c3f43$490f5f27$4c0aa7c0$...
    >
    >> (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.


    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. ------
    Lew Pitcher, Nov 3, 2008
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Bill Cunningham wrote:
    > 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-----
    Pawel Dziepak, Nov 3, 2008
    #6
  7. Bill Cunningham

    John Bode Guest

    On Nov 3, 2:14 pm, "Bill Cunningham" <> wrote:
    >     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+.
    John Bode, Nov 3, 2008
    #7
  8. On Mon, 3 Nov 2008 15:38:05 -0500, "Bill Cunningham"
    <> wrote:

    >
    >"Lew Pitcher" <> wrote in message
    >news:c3f43$490f5f27$4c0aa7c0$...
    >
    >> (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.


    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.

    --
    Remove del for email
    Barry Schwarz, Nov 4, 2008
    #8
  9. On Mon, 03 Nov 2008 21:26:31 +0100, Pawel Dziepak
    <> wrote:

    >-----BEGIN PGP SIGNED MESSAGE-----
    >Hash: SHA1
    >
    >Bill Cunningham wrote:
    >> 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


    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.

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


    --
    Remove del for email
    Barry Schwarz, Nov 4, 2008
    #9
  10. Bill Cunningham

    ruckuus Guest

    On Nov 4, 3:38 am, "Bill Cunningham" <> wrote:
    > "Lew Pitcher" <> wrote in message
    >
    > news:c3f43$490f5f27$4c0aa7c0$...
    >
    > > (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


    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.
    ruckuus, Nov 4, 2008
    #10
  11. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Barry Schwarz wrote:
    >> 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.


    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-----
    Pawel Dziepak, Nov 4, 2008
    #11
  12. On 4 Nov, 14:13, Pawel Dziepak <> wrote:
    > Barry Schwarz wrote:


    > -----BEGIN PGP SIGNED MESSAGE-----


    could we lose the PGP stuff?

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

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


    ITYM "handle". Despite what wikipedia says this is not standard C
    terminology


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


    --
    Nick Keighley
    Nick Keighley, Nov 4, 2008
    #12
  13. -----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-----
    Pawel Dziepak, Nov 4, 2008
    #13
  14. Bill Cunningham

    Chris Dollin Guest

    Pawel Dziepak wrote:

    > "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
    Chris Dollin, Nov 4, 2008
    #14
  15. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Chris Dollin wrote:
    > 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-----
    Pawel Dziepak, Nov 4, 2008
    #15
  16. On 4 Nov, 15:05, Chris Dollin <> wrote:

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

    --
    Nick Keighley
    Nick Keighley, Nov 4, 2008
    #16
  17. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Nick Keighley wrote:
    > 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-----
    Pawel Dziepak, Nov 4, 2008
    #17
  18. Bill Cunningham

    Chris Dollin Guest

    Pawel Dziepak wrote:

    > Nick Keighley wrote:
    >> 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.


    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
    Chris Dollin, Nov 4, 2008
    #18
  19. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Eric Sosman wrote:
    > 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-----
    Pawel Dziepak, Nov 4, 2008
    #19
  20. Bill Cunningham

    jameskuyper Guest

    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.
    jameskuyper, Nov 4, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto
    Replies:
    1
    Views:
    529
  2. Jason

    Authentication modes

    Jason, Aug 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    331
    Jason
    Aug 7, 2003
  3. Eidolon

    per location Authentication Modes?

    Eidolon, Feb 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    293
    Eidolon
    Feb 24, 2004
  4. Nonee
    Replies:
    2
    Views:
    2,638
    Neredbojias
    Oct 25, 2005
  5. Michel Rouzic
    Replies:
    4
    Views:
    1,823
    Michel Rouzic
    Apr 28, 2008
Loading...

Share This Page