Solved

Random File Update

Posted on 2003-10-29
3
189 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
Comment Utility
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
Comment Utility
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
Comment Utility

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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