Solved

Random File Update

Posted on 2003-10-29
3
191 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 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

21 Experts available now in Live!

Get 1:1 Help Now