I don't understand how to use the second parameter when used with +. Trying to open a file, read one line of text and then write one line of text before closing the file. The example below, once I step passed the first fopen line, it already erased the content of the file, before I can even read it.

$fh = fopen("file.txt", "w+");
$line = (int) fgets($fh);
$line++;
fwrite($fh, $line);
fclose($fh);

I read the different fopen paremeter explanations, for example, comparing w and w+, they both state "start from the beginning of the file, and erase the content of the file", the only difference is that w+ also has "Open"???

    As per the [man]fopen[/man] manual page:

    'w+' | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

    Try using 'r+', instead.

      I tried using r+, which in a way works, but it appends the new text to the eof. I'll play around with it to figure it out. All I'm trying to do is create a very simple counter file to keep track of how many times visitors have clicked on a submit button.

      Going back to the w+. I understand that it truncates the file to zero length, once fopen is executed. What I'm confused about is, since it removes all text, why the description state "open for Reading and Writing", how is it reading when it truncates the file first?

      What I like about w+ is that it will attempt to create the file if it does not exist.

        Maybe use 'a+', and then use [man]rewind/man if you want to move the pointer to the start of the file?

          Cool, the rewind is what I needed. Thanks NogDog 🙂

          Still baffled about the w+ "open for Reading and Writing", oh well.

            I think it's more likely to be used for stream I/O than it is for file I/O, but don't bet the house and farm on that. 😉

              "w+" could be that it creates a new file, and while keeping the stream open, you can read what you wrote.

                NogDog wrote:

                Maybe use 'a+', and then use [man]rewind/man if you want to move the pointer to the start of the file?

                The a+ didn't seem to work, even if I use rewind, it seems to insist of writing new data by appending to the eof. the r+ as you suggested previously, works fine along with rewind.

                  The + designation in the second parameter means "more" or write and read or read and write (depending upon the initial flag).

                  If you look at all the descriptions, you'll notice the extra "and writing" (or "reading and") added to it:

                  r :: reading only
                  r+ :: reading and writing
                  w :: writing only
                  w+ :: reading and writing
                  a :: writing only
                  a+ :: reading and writing
                  x :: writing only
                  x+ :: reading and writing

                  Now, r,w,a, and x will give different variations of what happens with the pointer once the file is opened.

                  Hope that helps explain it.

                    Write a Reply...