473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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
Sep 26 '08 #1
5 2479
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Shaoyong Wang | last post by:
Dear All, I want to write a simple PHP code to verify whether a given list of URLs is broken or not. The URLs given have various formats, for example, http://www.afro.com/history/history.html...
3
by: Ming | last post by:
When I use fopen on the URL: fopen("http://home.pchome.com.tw/world/qoo4ko/110.jpg";, "r") I would get the following error message: -----------Error Message------------ Warning: fopen():...
18
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers....
10
by: Longfellow | last post by:
Newbie here... My reading of the description of fopen() led me to expect that, with mode as "w", it would create a file if it did not exist. Checked the FAQ and did not see this question...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
2
by: Mike | last post by:
Hello, Im doing this in php5, apache Im not sure where the problem lies but I have a file <?php Class CreateXML{ public function xmlDeclaration(){ return $varxmlDec = "<?xml version='1.0'> ";...
17
by: vashwath | last post by:
#include <stdio.h> int main() { FILE *fp; char s; fopen("file.txt","w+"); fprintf(fp,"HI\n");
23
by: Anunay | last post by:
Hi all, Suppose a text file is given and we have to write a program that will return a random line from the file. This can be easily done. But what if the text file is too big and can't fit...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
8
by: Alien | last post by:
Hi, I have a question regarding the fopen( ) function. I wrote this piece of code: #include <stdio.h> int main() { FILE *fp;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.