Replacing a record in a flat file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am trying to replace a single record in a flat file.  The file is
relatively small and no need for database.

I open the file, and save the ftell() value in a variable.
I read a record using fgetcsv(), modify the index contents it if/as
I then position the pointer back to the beginning of that record using
I build a new string with the required delimiters.
Finally I put the record with fputs() (also fails with fwrite()).

What the code actually does, is append a NEW record to the end.  Can't seem
to find out hot to REPLACE the original record with the new data.

$fp = fopen('filename');
$filerec = ftell($fp);
$user = fgetcsv($fp, 999, "\t");
   Code here to change as required $user[0-6]
$outputstr = $user[0]."\t".$user[1]."\t". etc, etc
fputs($fp, $outputstr);

Thanks, bt3

Re: Replacing a record in a flat file

On Wed, 11 May 2005 20:48:35 -0500, in comp.lang.php "BT3"

Quoted text here. Click to load it

This is standard behaviour across all languages. Think of what is
happening. You have a tab delimited file and you want to change a
'record'. The current record is Sam and you want to change it to
Martina. So your current and updated record will look like:

Notice the delimiters, they've moved. What happens to the data in all
of the following records? Well, the data is overwritten by the new
'record' thus causing corruption of your data. So your old records may
look like:

The updated record will look like:

So now when you read in Bill Bloggs you are going to retrieve

There are 2 methods you can use to get around this problem
Method 1:
Read in the entire file
write out the data up the updated 'record'
insert the update record into the file
complete writing out the rest of the data.

(php may have a function that does this for you, I don't know).

Method 2:
Convert your file to a fixed width format.
create a structure that defines the 'fields' for each 'record'

This method will take exactly the same amount of space for each
'record'. So when you update:
Sam         Toocan      ,
Bill        Bloggs      ,
Wilbur      West        ,

Martina     Poppins     ,
Bill        Bloggs      ,
Wilbur      West        ,

There is no data corruption.

when updating the file you will need to pad out the fields to their
specified width
when reading the file you will need to remove the padding from each

Quoted text here. Click to load it

---------------------------------------------------------------  : Remove your pants to reply

Re: Replacing a record in a flat file

Jeff North wrote:
Quoted text here. Click to load it

Yea, databases are evil.. So why write one yourself?!

Quoted text here. Click to load it

Method 3:
Give each record a unique identifier, then just append changed records
to the bottom as you go. I assume you're working with the whole data
set, otherwise a flat file really isn't the way to go in the first place.

Method 4:
Use a frigging database. SQLite, Postgres, even mysql..

Site Timeline