• C

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.
sonic2000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 OlsenDBACommented:

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.