Loading....
Coupon Accepted Successfully!

 

Random Access Operation

Data files always contain a large amount of information, and the information always changes. The changed information should be updated; otherwise, the data files are not useful. Thus, to update data in the file, we need to update the data files with latest information. To update a particular record of the data file, the data may be stored anywhere in the file; it is necessary to obtain the location (in terms of byte number) at which the data object is stored.
 
The sizeof() operator determines the size of the object. Consider the following statements:
 

(a) int size = sizeof(o);

 
Here, o is an object, and size is an integer variable. The sizeof() operator returns the size of the object o in bytes, and it is stored in the variable size. Here, one object is equal to one record.
The position of the nth record or object can be obtained using the following statement:
 

(b) int p = (n-1 * size);

 
Here, p is the exact byte number of the object that is to be updated; n is the number of the object; and size is the size in bytes of an individual object (record).
 
Suppose we want to update the fifth record. The size of the individual object is 26.
 

(c) p = (5-1*26) i.e. p = 104

 
Thus, the fifth object is stored in a series of bytes from 105 to 130. Using seekg() and seekp() functions, we can set the file pointer at that position.
 

16.19 Write a program to create a text file. Add and modify records in the text file. The record should contain name, age, and height of a boy.

#include<stdio.h>

#include<process.h>

#include<fstream.h>

#include<conio.h>

class boys

{

char name [20];

int age;

float height;

public:

void input()

{

cout<< “Name:”; cin>>name;

cout<< “Age:”; cin>>age;

cout<< “Height:”; cin>>height;

}

void show (int r)

{

cout<<“\n”<<r<<“\t”<<name<<“\t”<<age <<“\t”<<height; }

};

boys b[3];

fstream out;

void main()

{

clrscr();

void menu (void);

out.open (“boys.doc”, ios::in | ios::out | ios::noreplace);

menu();

}

void menu(void)
{
void get(void);
void put(void);
void update(void);
int x;
clrscr();
cout<<“\n Use UP arrow key for selection”;
char ch=‘ ’;
gotoxy(1,3);
printf (“ADD()”);
gotoxy(1,4);
printf (“ALTER()”);
gotoxy(1,5);
printf (“EXIT()”);
x=3;
gotoxy(7,x);
printf (“*”);
while (ch!=13)
{
ch=getch();
if (ch==72)
{
if (x>4)
{
gotoxy(7,x);
printf (“ ”);
x=2;
}
gotoxy(7,x);
printf (“ ”);
gotoxy(7,++x);
printf (“*”);
}
}
switch(x)
{
case 3 : get(); put(); getche(); break;
case 4 : put(); update(); put(); getche(); break;
default exit(1);
}
menu();
}

void get()
{
cout<<“\n\n\n\n Enter following information:\n”;
for (int i=0;i<3;i++)
{
b[i].input();
out.write ((char*) & b[i],sizeof(b[i]));
}
}
void put()
{
out.seekg(0,ios::beg);
cout<<“\n\n\n Entered information \n”;
cout<<“Sr.no Name Age Height”;
for (int i=0;i<3;i++)
{
out.read((char *) & b[i],
sizeof(b[i]));
b[i].show(i+1);
}
}
void update()
{
int r, s=sizeof(b[0]);
out.seekg(0,ios::beg);
cout<<“\n”<<“Enter record no. to update:”;
cin>>r;
r=(r-1)*s;
out.seekg(r,ios::beg);
b[0].input();
out.write ((char*) & b[0],sizeof(b[0]));
put();
}

OUTPUT
Use UP arrow key for selection
ADD (*)
ALTER()
EXIT()
Enter following information :
Name : Sachin
Age : 28
Height : 5.4
Name : Rahul
Age : 28
Height : 5.5
Name : SauravAge : 29
Height : 5.4
Entered information
Sr.no Name Age Height
1 Sachin 28 5.4
2 Rahul 28 5.5
3 Saurav 29 5.4
 
Explanation: In the above program, the class boys contains the data member’s name, age, and height. The class boys also contains the member functions input() and show(). The input() function is used to read data, and the show() function displays data on the screen.
 
After class definition and before the main() function array of objects, b[3] and fstream object out are declared. They are declared before main() for global access. The file boys.doc is opened in the input and output modes to perform both read and write operations.
 
The menu() function displays the menu on the screen. The menu items can be selected using the up arrow key. Hit enter to start the operation. There are another three user-defined functions. They are get(), put(), and update(). The get() function calls the member functioninput() to read data through the keyboard. The get() function writes the data using the write() function. The put() function calls the member function show(). The put() function calls the member function show(). The put() function reads the data from the file using the read() function.
 
The update() function is used to modify the previous record. The seekg() function sets the file pointer at the beginning of the file. The sizeof() operator determines the size of the object and stores it in the variable s.




Test Your Skills Now!
Take a Quiz now
Reviewer Name