472,978 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Replacing a record in a flat file

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
2 3427
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
3
by: Earl Teigrob | last post by:
I am considering writing a Class that Selects, Adds, Updates and Deletes Nodes in an XML File but do not what to reinvent the wheel. (See XML file below) That data format would emulate records...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
1
by: new | last post by:
I have data for each week in a single table. I need to export this data to a separate flat file for each week. Any ideas? DB2 SQL Query export to flat files as a function of data on each record
0
by: svallarian | last post by:
C# beginner here. I'm writing serialized records to a file, and I'm able to add and read them back to the program no problem. However, when I try to modify the record by reading the record,...
6
by: deppeler | last post by:
How do I extract the value of the LAST record entered for the field $id (just the one field) from a flat file DB? These are my fields:...
1
by: rhector31 | last post by:
Hi, I have this file that some lines are related to the same record. I need to analize it but I need to create a flat file. I want to every time that the record start with an ENT split it and add...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.