Solved

Random File Update

Posted on 2003-10-29
3
192 Views
Last Modified: 2010-04-15
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
Comment
Question by:sonic2000
3 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 50 total points
ID: 9648368
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
 
LVL 6

Expert Comment

by:Ajar
ID: 9649969
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9651274

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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now