Question about fopen() and r+

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

    #1

    Question about fopen() and r+

    I am reading the docs on fopen() and think I want to use the r+ option
    so I can read in a file, make changes to the data, write the changes
    back, overwriting the original data. I believe using flock() will lock
    the file while I am doing this (is this so?) so no one else can access
    it. The question I have is, in the docs it says it sets the file
    pointer to the begining of the file for reading, but where is the
    filepointer set to for writing, or does it over write the existing
    information as soon as I write back to the file?

    Any help, pointers, etc appreciated

    Bill H
  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: Question about fopen() and r+

    Bill H wrote:
    I believe using flock() will lock the file while I am doing this (is this
    so?) so no one else can access it.
    Nope - please re-read the PHP manual. flock() is an *optimistic* locking
    solution.
    The question I have is, in the docs it says it sets the file
    pointer to the begining of the file for reading, but where is the
    filepointer set to for writing, or does it over write the existing
    information as soon as I write back to the file?
    To the beginning in write mode, to the end in append mode. Please RTFM:


    --
    ----------------------------------
    Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Bill H

      #3
      Re: Question about fopen() and r+

      On Sep 26, 8:54 am, Iván Sánchez Ortega <ivansanchez-...@rroba-
      escomposlinux.-.punto.-.orgwrote:
      Bill H wrote:
      I believe using flock() will lock the file while I am doing this (is this
      so?) so no one else can access it.
      >
      Nope - please re-read the PHP manual. flock() is an *optimistic* locking
      solution.
      >
      The question I have is, in the docs it says it sets the file
      pointer to the begining of the file for reading, but where is the
      filepointer set to for writing, or does it over write the existing
      information as soon as I write back to the file?
      >
      To the beginning in write mode, to the end in append mode. Please RTFM:http://php.net/fopen
      >
      --
      ----------------------------------
      Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-
      >
      Un ordenador no es un televisor ni un microondas, es una herramienta
      compleja.
      Ivan

      I am asking about the "r+" mode, I understand the w+ and a+, but the r
      + is vague

      The manual says: 'r+' Open for reading and writing; place the file
      pointer at the beginning of the file. My question was - does this mean
      that any writing starts at the begining of the file also and
      overwrites existing data? Or is any data that is writen placed where
      you left off reading?

      Bill H

      Comment

      • Captain Paralytic

        #4
        Re: Question about fopen() and r+

        On 26 Sep, 15:54, Bill H <b...@ts1000.us wrote:
        On Sep 26, 8:54 am, Iván Sánchez Ortega <ivansanchez-...@rroba-
        >
        >
        >
        >
        >
        escomposlinux.-.punto.-.orgwrote:
        Bill H wrote:
        I believe using flock() will lock the file while I am doing this (is this
        so?) so no one else can access it.
        >
        Nope - please re-read the PHP manual. flock() is an *optimistic* locking
        solution.
        >
        The question I have is, in the docs it says it sets the file
        pointer to the begining of the file for reading, but where is the
        filepointer set to for writing, or does it over write the existing
        information as soon as I write back to the file?
        >
        To the beginning in write mode, to the end in append mode. Please RTFM:http://php.net/fopen
        >
        --
        ----------------------------------
        Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-
        >
        Un ordenador no es un televisor ni un microondas, es una herramienta
        compleja.
        >
        Ivan
        >
        I am asking about the "r+" mode, I understand the w+ and a+, but the r
        + is vague
        >
        The manual says: 'r+' Open for reading and writing; place the file
        pointer at the beginning of the file. My question was - does this mean
        that any writing starts at the begining of the file also and
        overwrites existing data? Or is any data that is writen placed where
        you left off reading?
        Why didn't you just try it?

        Comment

        • Janwillem Borleffs

          #5
          Re: Question about fopen() and r+

          Bill H schreef:
          I am asking about the "r+" mode, I understand the w+ and a+, but the r
          + is vague
          >
          The manual says: 'r+' Open for reading and writing; place the file
          pointer at the beginning of the file. My question was - does this mean
          that any writing starts at the begining of the file also and
          overwrites existing data? Or is any data that is writen placed where
          you left off reading?
          >
          This means that the file is truncated (emptied) before writing. It also
          implies that the result of the following is an empty file:

          $fp = fopen('file', 'r+');
          fclose($fp);


          JW

          Comment

          • Jamie Furness

            #6
            Re: Question about fopen() and r+

            Bill H wrote:
            On Sep 26, 8:54 am, Iván Sánchez Ortega <ivansanchez-...@rroba-
            escomposlinux.-.punto.-.orgwrote:
            >Bill H wrote:
            >>I believe using flock() will lock the file while I am doing this (is this
            >>so?) so no one else can access it.
            >Nope - please re-read the PHP manual. flock() is an *optimistic* locking
            >solution.
            >>
            >>The question I have is, in the docs it says it sets the file
            >>pointer to the begining of the file for reading, but where is the
            >>filepointer set to for writing, or does it over write the existing
            >>information as soon as I write back to the file?
            >To the beginning in write mode, to the end in append mode. Please RTFM:http://php.net/fopen
            >>
            >--
            >----------------------------------
            >Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-
            >>
            >Un ordenador no es un televisor ni un microondas, es una herramienta
            >compleja.
            >
            Ivan
            >
            I am asking about the "r+" mode, I understand the w+ and a+, but the r
            + is vague
            >
            The manual says: 'r+' Open for reading and writing; place the file
            pointer at the beginning of the file. My question was - does this mean
            that any writing starts at the begining of the file also and
            overwrites existing data? Or is any data that is writen placed where
            you left off reading?
            >
            Bill H
            The file pointer is used for both the reading and writing position, data
            you write will be placed where you left off reading. If you want to
            overwrite it you will need to use fseek() to move the pointer back to
            the start.

            Jamie

            Comment

            Working...