fopen modes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    fopen modes

    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


  • Pawel Dziepak

    #2
    Re: fopen modes

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

    iEYEARECAAYFAkk PXncACgkQPFW+cU iIHNr6iACfZ0wkb BTvQQKyw/8D4kPzDARp
    wjMAoIxZbrkMTAY 487oUXsqHp9dEwb pz
    =iUvy
    -----END PGP SIGNATURE-----

    Comment

    • Lew Pitcher

      #3
      Re: fopen modes

      On November 3, 2008 15:14, in comp.lang.c, Bill Cunningham
      (nospam@nspam.i nvalid) 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. ------


      Comment

      • Bill Cunningham

        #4
        Re: fopen modes


        "Lew Pitcher" <lpitcher@teksa vvy.comwrote in message
        news:c3f43$490f 5f27$4c0aa7c0$5 627@TEKSAVVY.CO M...
        (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


        Comment

        • Lew Pitcher

          #5
          Re: fopen modes

          On November 3, 2008 15:38, in comp.lang.c, Bill Cunningham
          (nospam@nspam.i nvalid) wrote:
          >
          "Lew Pitcher" <lpitcher@teksa vvy.comwrote in message
          news:c3f43$490f 5f27$4c0aa7c0$5 627@TEKSAVVY.CO M...
          >
          >(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. ------


          Comment

          • Pawel Dziepak

            #6
            Re: fopen modes

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

            iEYEARECAAYFAkk PY4IACgkQPFW+cU iIHNpRFQCgpGU23 oMfF3bKEzb8P0mm oqpN
            /MAAnifDbXcnJzDG S3EOEQNACQias8k u
            =Mtwi
            -----END PGP SIGNATURE-----

            Comment

            • John Bode

              #7
              Re: fopen modes

              On Nov 3, 2:14 pm, "Bill Cunningham" <nos...@nspam.i nvalidwrote:
                  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+.

              Comment

              • Barry Schwarz

                #8
                Re: fopen modes

                On Mon, 3 Nov 2008 15:38:05 -0500, "Bill Cunningham"
                <nospam@nspam.i nvalidwrote:
                >
                >"Lew Pitcher" <lpitcher@teksa vvy.comwrote in message
                >news:c3f43$490 f5f27$4c0aa7c0$ 5627@TEKSAVVY.C OM...
                >
                >(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

                Comment

                • Barry Schwarz

                  #9
                  Re: fopen modes

                  On Mon, 03 Nov 2008 21:26:31 +0100, Pawel Dziepak
                  <pdziepak@quarn os.orgwrote:
                  >-----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
                  >
                  >iEYEARECAAYFAk kPXncACgkQPFW+c UiIHNr6iACfZ0wk bBTvQQKyw/8D4kPzDARp
                  >wjMAoIxZbrkMTA Y487oUXsqHp9dEw bpz
                  >=iUvy
                  >-----END PGP SIGNATURE-----
                  --
                  Remove del for email

                  Comment

                  • ruckuus

                    #10
                    Re: fopen modes

                    On Nov 4, 3:38 am, "Bill Cunningham" <nos...@nspam.i nvalidwrote:
                    "Lew Pitcher" <lpitc...@teksa vvy.comwrote in message
                    >
                    news:c3f43$490f 5f27$4c0aa7c0$5 627@TEKSAVVY.CO M...
                    >
                    (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.

                    Comment

                    • Pawel Dziepak

                      #11
                      Re: fopen modes

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

                      iEYEARECAAYFAkk QWJsACgkQPFW+cU iIHNqWAwCfeT9N3 SowvbP7FQTTNEjJ GpHg
                      SQkAn1EBmiPUK3L LM8WE+WbZPJJp5e px
                      =lzKc
                      -----END PGP SIGNATURE-----

                      Comment

                      • Nick Keighley

                        #12
                        Re: fopen modes

                        On 4 Nov, 14:13, Pawel Dziepak <pdzie...@quarn os.orgwrote:
                        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.or g/wiki/File_descriptor
                        --
                        Nick Keighley

                        Comment

                        • Pawel Dziepak

                          #13
                          Re: fopen modes

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

                          iEYEARECAAYFAkk QXkUACgkQPFW+cU iIHNr81QCfYlAOf vMsQzFMAQ/9V6C4Aq3a
                          ZJAAoJFxGFzRUCC e6SGoWtJbESWDOH 3L
                          =WmWa
                          -----END PGP SIGNATURE-----

                          Comment

                          • Chris Dollin

                            #14
                            Re: fopen modes

                            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

                            Comment

                            • Pawel Dziepak

                              #15
                              Re: fopen modes

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

                              iEYEARECAAYFAkk QZsMACgkQPFW+cU iIHNrBdQCfZMTIP P+CL/X0z0xsGxNIYVNy
                              oNMAnj/zxaFVC0NVHU8bIt bWwcwttMqN
                              =VsgU
                              -----END PGP SIGNATURE-----

                              Comment

                              Working...