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

Replacing a record in a flat file

P: n/a
BT3
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.

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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 11 May 2005 20:48:35 -0500, in comp.lang.php "BT3"
<ho******@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
|


---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 17 '05 #2

P: n/a
Jeff North wrote:
On Wed, 11 May 2005 20:48:35 -0500, in comp.lang.php "BT3"
<ho******@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..
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.