728x90 AdSpace

Latest Article



Read or Write on File in C C++ Student Update Search Record Example Code




IO Operation Read or Write on File Student Record C C++ Example Code  Programming Language Implementation  Write a program that read a student name and class and store in file user can update record search record write record delete record example code :





#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class person{
char name[40];
char clas[40];
public:
void getdata()
{
cout<<"\nEnter a Name :";cin>>name;
        cout<<"Enter a Class :";cin>>clas;
}

void showdata()
{
cout<<"\nName :"<<name;
        cout<<"\nClass :"<<clas;
}
};


void update()
{
//////////////////////UPDATE IN FILE///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
  long int pos;
  int no;
  person student;
  fstream upfile;
  upfile.open("stud.txt",ios::in|ios::out|ios::binary);
  upfile.seekg(0L,ios::beg);
  cout<<"\nEnter a student Reg no for Update : ";cin>>no;
  student.getdata();
  pos=(no-1)*sizeof(student);
  upfile.seekp(pos,ios::beg);
  upfile.write((char *)&student,sizeof(student));
  cout<<endl;
}
void search()
{
//////////////////////SEARCH IN FILE///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
  long int pos;
  int no;
  person student;
  fstream file;
  file.open("stud.txt",ios::app|ios::in|ios::out|ios::binary);
  cout<<"\nEnter a student Reg no for Search:";cin>>no;
  pos=(no-1)*sizeof(student);
  file.seekg(pos);
  file.read((char *)&student,sizeof(student));
  student.showdata();
  cout<<endl;
  getch();
}
void newrecord()
{
///////////////////////WRITE IN FILE///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
  char ch;
  person student;
  fstream file;
  file.open("stud.txt",ios::app|ios::in|ios::out|ios::binary);
  file.seekg(0L,ios::end);
  int regno=(file.tellg()/sizeof(student))+1;
  cout<<"Enter a record of Student Reg no :"<<regno;
  student.getdata();
  file.write((char *)&student,sizeof(student));
}
void main()
{
  int chno;
  do{
     clrscr();
     cout<<"\n***Main Menu***";
     cout<<"\n1: New record.";
     cout<<"\n2: Search record.";
     cout<<"\n3: Update record.";
     cout<<"\n4: Exit.";
     cout<<"\nEnter your choice no :";cin>>chno;
switch(chno)
{
  case 1:
 newrecord();
 break;
  case 2:
  search();
  break;
  case 3:
  update();
  break;
   case 4:
  exit(0);
 
}

  }while(chno<4);


}



no image
  • Title : Read or Write on File in C C++ Student Update Search Record Example Code
  • Posted by :
  • Date : 07:28
  • Labels :






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment