HANDLING SEQUENTIAL ACCESS FILE


SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
 char name[10];
 int code;
 float cost;
public:
  void readdata(void);
  void writedata(void);
};
void INVENTORY::readdata(void)
 {
    cout<<"Enter name: ";
    cin>>name;
    cout<<"Enter code: ";
    cin>>code;
    cout<<"Enter cost: ";
    cin>>cost;
 }
void INVENTORY::writedata(void)
 {
    cout<<setiosflags(ios::left)<<setw(10)<<name<<setiosflags(ios::right)<<setw(10)<<code
        <<setprecision(2)<<setw(10)<<cost<<endl;
 }
void main()
{
 clrscr();
 INVENTORY item[3];
 fstream file;
 file.open("STOCK.DAT",ios::in|ios::out);
 cout<<"ENTER DETAILS FOR THREE ITEMS\n";
 for(int i=0;i<3;i++)
  {
   item[i].readdata();
   file.write((char *)&item[i],sizeof(item[i]));
  }
 file.seekg(0);
 cout<<"\nOUTPUT\n\n";
 for(i=0;i<3;i++)
  {
   file.read((char *)&item[i],sizeof(item[i]));
   item[i].writedata();
  }
  file.close();
  getch();
}

OUTPUT:

ENTER DETAILS FOR THREE ITEMS
Enter name:C++
Enter code:101
Enter cost:175
Enter name:FORTRAN
Enter code:102
Enter cost:150
Enter name:JAVA
Enter code:115
Enter cost:225

OUTPUT

C++                    101       175
FORTRAN        102       150
JAVA                  115       225
Previous
Next Post »

Still not found what you are looking for? Try again here.