473,807 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_fro m_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 3478
On Wed, 11 May 2005 20:48:35 -0500, in comp.lang.php "BT3"
<ho******@epmct c.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_fro m_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\tPoppin s\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\tPoppin s\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******@yourpa ntsyahoo.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******@epmct c.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\tPoppin s\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\tPoppin s\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
3431
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 using a flat file to storing data. I have to do this because mySQL is not installed on my web server, and I am not the root user. The amount of data is so small, that it isn't worth a full-blown database anyway. However, while the data is...
3
3329
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 in a Database Table with a Primary Key for each Record (see xml sample below) and a flat file structure. I would use a class to manipulate this type of data structure extensivly if I had one. Does anyone know of such an animal?
5
3350
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 I'm adding a customer. The Customer fields are mostly foreign keys that refer to primary keys in other tables, left join instead of junction tables at this point. So, when I want to add a customer record, I also need to add records to the other...
22
3021
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 files, which in turn could be simply and easily maintained with notepad. I understand the benefits of XML, really, but in the case of configuration files it seems it is almost always nothing more than unnecessary complexity, both in accessing them...
1
5555
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
1054
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, modifying it in memory, then writing it back to the file, the file gets written out as the NEXT file in the record. What I want to do is "roll back" the filestream to the previous record,
6
2445
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: "$snumber,$kit,$id,$'item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in\n"; thanks!
1
2330
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 to the right the records below the ENT. ENT*3*2J*34*555555555~ NM1*QE*1*FIGUEROA~ RMR*11*DB020920002010080013644378028**0~ DTM*582****RD8*20060801-20060831~ RMR*IK*47A0612**0*0~ ADX*.00*IA~ ENT*4*2J*34*999999999~ NM1*QE*1*FIGUEROA~
15
5283
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 show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.