Sie sind auf Seite 1von 11

COLLEGE OF TECHNOLOGY AND ENGINEERING

MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY


UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

PROGRAM 1
AIM:Write a program to handle fixed length records. Perform following operations. 1) Insert record. 2) Delete
Record. 3) Modify Record. 4) Display records.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
#include <process.h>
#include <stdio.h>
class student{
char name[20],address[40],ogpa[10], id[10];
public:
void insert();
void getdata();
void delete_record();
void showdata();
char *getid(){return id;}
void update(char *nm,char *add, char *grade){
strcpy(name,nm);
strcpy(address,add);
strcpy(ogpa,grade);
}
void deleter(){
strcpy(id,'\0');
strcpy(name,'\0');
strcpy(address,'\0');
strcpy(ogpa,'\0');
cout<<"\n Record Deleted";
}
}rec,s1;
void student :: getdata(){
cout<<"\nEnter ID : ";
gets(id);
cout<<"\nEnter Name : ";
gets(name);
cout<<"\nEnter ogpa : ";
gets(ogpa);
cout<<"\nEnter address : ";
gets(address);
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

void student :: showdata(){


cout<<setw(15)<<id;
cout<<setw(15)<<name;
cout<<setw(15)<<ogpa;
cout<<setw(25)<<address;
}
void student::insert(){
fstream file;
file.open("record.txt",ios::in|ios::out|ios::ate);
file.seekp(0,ios::end);
rec.getdata();
file.write((char *)&rec,sizeof(rec));
file.close();
}
void main(){
fstream file;
file.open("record.txt", ios::ate | ios::in | ios::out | ios::binary);
char ch,add[30],nm[30],rn[10],grade[10];
int choice,found=0;
while(1){
cout<<"\n Student Record \n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Record by ID\n";
cout<<"4) Delete a record\n";
cout<<"5) Modify Record\n";
cout<<"6) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice){
case 1 :
int size=sizeof(rec);
long pos;
int flag=0;
file.seekg(0,ios::beg);
while(file.read((char *)&rec,sizeof(rec))) {
if((strcmp(rec.getid(),'\0')==0)){
pos=file.tellg();
file.seekp(pos-size);
rec.getdata();
file.write((char *) &rec, sizeof(rec));
flag=1;
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

cout<<"\n Record added.. \n";


getch();
break;
} }
if(!flag) {
rec.insert();
cout<<"\n Record added.. \n";
getch();
break;
}
break;
case 2 :
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Student Record\n";
while(file){
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear();
getch();
break;
case 3 :
cout<<"\n\nEnter ID : ";
gets(rn);
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec))){
if(strcmp(rec.getid(),rn)==0){
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;
case 4:
cout<<"\n\nEnter ID : ";
cin>>rn;
file.seekg(0,ios::beg);
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec))){
cnt++;
if(strcmp(rec.getid(),rn)==0){
found=1;
break;
}
}
file.clear();
if(found==0){
cout<<"\n\n---Record Not found---\n";
getch();
break;
}
else{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
file.seekp(location);
rec.deleter();
file.write((char *) &rec, sizeof(rec));
getch();
break;
file.flush();
}
break;
case 5 :
cout<<"\n\nEnter ID : ";
cin>>rn;
file.seekg(0,ios::beg);
found=0;
cnt=0;
while(file.read((char *) &rec, sizeof(rec))){
cnt++;
if(strcmp(rec.getid(),rn)==0){
found=1;
cout<<"\nThe old values of the record with ID "<<rn<<" are \n ";
rec.showdata();
break;
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

}
file.clear();
if(found==0)
{ cout<<"\n\n---Record Not found---\n";getch(); break;}
else{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"\nEnter new Name : ";
cin>>nm;
cout<<"Enter new Address : ";
cin>>add;
cout<<"Enter new ogpa : ";
cin>>grade;
file.seekp(location);
rec.update(nm,add,grade);
file.write((char *) &rec, sizeof(rec));
file.flush();
cout<<"\n Record Modified \n";
getch();
break;
}
break;
case 6 : exit(0);
}
}
file.close();
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

PROGRAM 2
AIM:Write a program to handle variable length records. Perform following operations. 1) Insert record. 2)
Delete Record. 3) Modify Record. 4) Display records.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
class student{
int rn;
char name[20];
char add1[20];
char add2[20];
public:
void insert();
void display();
void del();
void modify();
}s;

void student::insert()
{
ofstream of("student.txt",ios::app);
cout<<"Rollno: "; cin>>rn;
of<<"$"<<rn<<"#"; cin.ignore();
cout<<"Name: "; cin.getline(name,20);
of.write(name,strlen(name));
of<<"#";
cout<<"Address 1 : "; cin.getline(add1,20);
of.write(add1,strlen(add1));
of<<"#";
cout<<"Address 2 : "; cin.getline(add2,20);
of.write(add2,strlen(add2));
of<<"#%";
of.close();
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

void student::display()
{
ifstream fi("student.txt",ios::in);
char c;
while(!fi.eof())
{
fi>>c;
if(c=='$'||c=='%')
cout<<"\n";
else if(c=='#')
cout<<"\t";
else if(c=='0')
{
cout<<"\n\n";
while(c!='$')
fi>>c;
}
else
cout<<c;
}
fi.close();
}

void student::del()
{
char roll;
char c;
cout<<"\nEnter rollno. to be deleted: ";
cin>>roll;
fstream fin("student.txt",ios::in|ios::out);
while(!fin.eof())
{
long pos=fin.tellg();
fin>>c;
if(c==roll)
{
fin.seekg(pos);
fin<<"0";
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

break;
}
}
fin.close();
cout<<"\nRecord deleted";
}
void student::modify()
{
char roll;
char c, name1[10], addr[20], addr2[20];
cout<<"\nEnter rollno. to be modified: ";
cin>>roll;
fstream fin("student.txt",ios::in|ios::out);
while(!fin.eof())
{
long pos=fin.tellg();
fin>>c;
if(c==roll)
{
fin.seekg(pos);
fin<<"0";
break;
}
}
fin.close();
ofstream of("student.txt",ios::app);
cout<<" enter the new Rollno: "; cin>>rn;
of<<"$"<<rn<<"#"; cin.ignore();
cout<<"Enter the new Name: "; cin.getline(name,20);
of.write(name,strlen(name));
of<<"#";
cout<<"Enter new Address 1 : "; cin.getline(add1,20);
of.write(add1,strlen(add1));
of<<"#";
cout<<"Enter new Address 2 : "; cin.getline(add2,20);
of.write(add2,strlen(add2));
of<<"#%";
of.close();
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

}
int main()
{ int ch;
while(1)
{
cout<<"\n1.Insert \n2.Display \n3.Delete \n4.Modify \n5.Exit\n";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:s.insert();
break;
case 2:s.display();
break;
case 3:s.del();
break;
case 4:s.modify();
break;
case 5:exit(0);
default:cout<<"Wrong choice";
break;
} } }
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

Das könnte Ihnen auch gefallen