By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Question about fopen() and r+

P: n/a
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
Sep 26 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Sep 26 '08 #2

P: n/a
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
Sep 26 '08 #3

P: n/a
On 26 Sep, 15:54, Bill H <b...@ts1000.uswrote:
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?
Sep 26 '08 #4

P: n/a
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
Sep 26 '08 #5

P: n/a
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
Sep 26 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.