• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Random File Update

I know how to do file read/write.
But then how to do random file write? That's to say if I were to write record name "John" together with his salary and if "John" already exists as one of the record in the file, how to update that field with the new salary without re-writing the entire file?

Just need an example.
0
sonic2000
Asked:
sonic2000
1 Solution
 
Sys_ProgCommented:
I think if your record is fixed, i.e. u know that your name would take 100 bytes, after that the salary field starts and it is occupying 4 bytes,

Then you can use
fseek () function to go to a particular desired position in file and then write to it
0
 
AjarCommented:
suppose your file 'sal.dat' has fixed length records like  name 32 bytes followed by salary int which is 4 bytes and the record repeated again

<----NAME 32-----><----salary 4------><----NAME 32-----><----salary 4------><----NAME 32-----><----salary 4------>
alice              xxxx                JHON             xyxx                fernendo          zxzx


if you want to change salary of JHON to  zzzz (you wont be able to read the actual int representation on file )
then do following

#include <stdio.h>
function change_salary(char * name ,int salary)
{
char t_name[32];
FILE  * f = fopen("sal.dat","rw");
while(!feof(f))
{
fread(t_name,32,1,f);
//now you read the whole name field into t_name
//compare it to find if the current t_name == name
if(strcmp (t_name,name) ==0) {fwrite(&salary,sizeof(int),1,f); break;} //name found write the salary
}
fclose(f);
}
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi sonic2000,

If you're going to do random I/O on a file, pick a granularity that makes sense for your application.  Based on your example, write ONLY fixed length records to the file.  This is pretty easy to do if you'll define a struct that describes your data and always read/write the entire struct.  While you can read/write smaller quantities, you add a lot of coding and overhead to manage what to update and when.  (Often times several fields must be updated in unison.  Updating them one-at-a-time runs the risk of the program updating only some of the fields and the data becoming meaningless.  Consider a change-of-address.  If only the street address and city are updated then all of the address fields are meaningless since neither the old address nor the new address are on file.)

Here's a bit of code to manage the I/O:

typedef struct
{
  char Name[32];
  float Salary;
}  UserData_t;

int  FileHandle;

ReadUserRecord (int RecordNumber, UserData_t *UserData)
{
  seek (FileHandle, RecordNumber * sizeof (UserData_t), SEEK_SET);
  read (FileHandle, UserData, sizeof (UserData_t));
}

WriteUserRecord (int RecordNumber, UserData_t *UserData)
{
  seek (FileHandle, RecordNumber * sizeof (UserData_t), SEEK_SET);
  write (FileHandle, UserData, sizeof (UserData_t));
}


These functions allow your program can randomly access any record in  the file.  They should, of course, do sanity checks and indicate whether they worked.


Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now