You are here: Re: Replacing a record in a flat file « PHP Programming Language « IT news, forums, messages
Re: Replacing a record in a flat file

Posted by Jeff North on 05/12/05 09:31

On Wed, 11 May 2005 20:48:35 -0500, in comp.lang.php "BT3"
<honeypot@epmctc.com> wrote:

>| 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
>| necessary.
>| I then position the pointer back to the beginning of that record using
>| fseek(value_from_ftell).
>| 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.

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:
Sam\tToocan\t
Martina\tPoppins\t

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:
Sam\tToocan\t
Bill\tBloggs\t
Wilbur\tWest\t

The updated record will look like:
Martina\tPoppins\t
Bloggs\t
Wilbur\tWest\t

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

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 ,

to
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
field



>| Code:
>| ---------
>| $fp = fopen('filename');
>| $filerec = ftell($fp);
>| -->for...next
>| $user = fgetcsv($fp, 999, "\t");
>| /*
>| Code here to change as required $user[0-6]
>| */
>| $outputstr = $user[0]."\t".$user[1]."\t". etc, etc
>| fseek($filerec);
>| fputs($fp, $outputstr);
>| -->next
>| fclose($fp);
>|
>| Thanks, bt3
>|

---------------------------------------------------------------
jnorthau@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация