|
Posted by Frank on 05/12/05 12:29
Jeff North wrote:
> 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.
>>| *snip*
>>| 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.
Yea, databases are evil.. So why write one yourself?!
> 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'
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..
Navigation:
[Reply to this message]
|