PROGRAM-1
#include<iostream.h>
#include<fstream.h>
#include<string.h>
void swap (char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}
void reverse(char *name)
{
int i,j,size;
size=strlen(name);
for(i=0,j=size-1;i<j;i++,j--)
swap(name[i],name[j]);
}
void main(int argc,char *argv[])
{
char name[30];
if(argc==1)
{
do
{
cout<<"enter name\n";
cin>>name;
reverse(name);
cout<<name;
}
while(!cin.eof());
}
else if(argc==3)
{
fstream ip,op;
ip.open(argv[1],ios::in);
op.open(argv[2],ios::out|ios::app);
do
{
ip>>name;
reverse(name);
cout<<name<<endl;
op<<name;
if(ip.eof())
break;
}
while(1);
}
else
cout<<"error";
}
// ctrl-c to exit !!!
Execution of file -> 1.CPP
1.start ->type cmd .
2. In commad Prompt type -> d :-> cd debug ->
3.Create 2 files 1.txt(Enter some names ) and 2.txt (keep it empty )
4. After executing program there will be a file called 1.exe in debug folder.
5. Type in 1.exe 1.txt 2.txt ( Reversed names will be found in 2.txt )
========================================
========================================
PROGRAM-2
Write a C++ program to read and write student objects with fixedlength
records and the fields delimited by “|”. Implement pack ( ),
unpack ( ), modify ( ) and search ( ) methods.
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdio.h>
class FLB;
class person
{
char usn[30],name[30],address[30],branch[30],college[30];
public:
void input();
void output();
void search(char *fname);
void modify(char *fname);
friend class FLB;
};
class FLB
{
char buff[160];
public:
FLB()
{
for(int i=0;i<160;i++)
buff[i]='\0';
}
void pack(person &p);
void unpack(person &p);
void read(fstream &fs);
void write(char *fname);
};
void person::input()
{
cout<<"enter usn,name,address,branch and college\n";
cin>>usn>>name>>address>>branch>>college;
}
void person::output()
{
cout<<"usn:"<<usn<<"\nname:"<<name<<"\naddress:"<<address<<"\nbranch:"<<branch<<"\ncollege:"<<college;
}
void FLB::pack(person &p)
{
strcpy(buff,p.usn);strcat(buff,"|");
strcat(buff,p.name);strcat(buff,"|");
strcat(buff,p.address);strcat(buff,"|");
strcat(buff,p.branch);strcat(buff,"|");
strcat(buff,p.college);
}
void FLB::unpack(person &p)
{
char *ptr=buff;
while(*ptr)
{
if(*ptr=='|')
*ptr='\0';
ptr++;
}
ptr=buff;
strcpy(p.usn,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.name,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.address,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.branch,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.college,ptr);
}
void FLB::read(fstream &fs)
{
fs.read(buff,sizeof(buff));
}
void FLB::write(char *fname)
{
fstream os(fname,ios::app);
os.write(buff,sizeof(buff));
os.close();
}
void person::search(char *fname)
{
int found=0;
person p;
FLB b;
char key[30];
fstream is(fname,ios::in);
cout<<"enter the usn to be searched";
cin>>key;
while((!is.eof())&&(!found))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if(strcmp(p.usn,key)==0)
{
cout<<"record found\n";
p.output();
found=1;
}
}
if(!found)
cout<<"does not exists\n";
is.close();
}
void person::modify(char *fname)
{
int found=0;
person p;
FLB b;
char key[30],tname[]="temp.txt";
fstream is(fname,ios::in);
fstream tfile(tname,ios::out|ios::app);
cout<<"enter the usn to be searched";
cin>>key;
while((!is.eof()))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if((strcmp(p.usn,key)==0)&&(!found))
{
cout<<"record found\n";
p.input();
found=1;
}
b.pack(p);
b.write(tname);
}
if(!found)
cout<<"does not exists\n";
is.close();
tfile.close();
//remove(fname);
//rename(tname,fname);
}
void main()
{
person p;
FLB b;
int ch;
char fname[]="prg2.txt";
do
{
cout<<"1.insert\t 2.search\t 3.modify\t 4.exit\n enter ur choice\n";
cin>>ch;
switch(ch)
{
case 1: p.input();
b.pack(p);
b.write(fname);
break;
case 2: p.search(fname);
break;
case 3: p.modify(fname);
break;
case 4: //remove(fname);
break;
}
}while(ch!=4);
=========================================
=========================================
PROGRAM -3
#include<fstream.h>
#include<stdio.h>
#include<string.h>
int n, no_of_rec, length, len[20];
class student
{
public:
char usn[20], sem[20], name[20], branch[20];
};
class textbuff
{
public:
char buff[500];
void pack(student s[])
{
fstream file1("first.txt", ios::out | ios::app);
clear_buff();
for(int i = 0; i < n; i++)
{
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
}
length = strlen(buff);
file1<<buff;
file1.close();
}
void unpack(student s[])
{
fstream file2("first.txt", ios::in);
clear_buff();
for(int i = 0; i < no_of_rec; i++)
{
file2.getline(s[i].usn, 20, '|');
file2.getline(s[i].name, 20, '|');
file2.getline(s[i].branch, 20, '|');
file2.getline(s[i].sem, 20, '|');
}
}
void clear_buff()
{
for(int i = 0; i < 500; i++)
buff[i] = NULL;
}
void display(student s[])
{
char ch;
if(no_of_rec != 0)
{
cout<<"USN\tName\tBranch\tSem\n";
for(int i = 0; i < no_of_rec; i++)
{
ch = s[i].usn[0];
if(ch != '*')
cout<<s[i].usn<<"\t"<<s[i].name<<"\t"<<s[i].branch<<"\t"<<s[i].sem<<endl;
}
}
else
cout<<"No record found\n";
}
void search(student s[])
{
int flag = 0;
char key[20];
clear_buff();
cout<<"Enter the USN for search: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
cout<<"Record found!!\n";
cout<<"USN: "<<s[i].usn<<", Name: "<<s[i].name<<", Branch: "<<s[i].branch<<", Sem: "<<s[i].sem<<endl;
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
void modify(student s[])
{
char key[20], cvar[20];
/*int len1[20];*/
int flag = 0, len_field;
/*for(int i = 1; i <= no_of_rec; i++)
len1[i] = len[i] - len[i-1];*/
cout<<"Enter the USN of record to be modified: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
fstream file3("first.txt", ios::in | ios::out);
fstream file4("first.txt", ios::out | ios::app);
file3.seekg(len[i], ios::beg);
file3.getline(cvar, 20, '|');
len_field = strlen(cvar); //length of 1 field
file3.seekp(len[i], ios::beg);
for(int j = 0; j < len_field; j++)
file3<<'*';
file3.close();
cout<<"Enter new data:\nEnter USN, name, branch and sem:\n";
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;
clear_buff();
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
// file3.seekp(0, ios::end);
file4<<buff;
file4.close();
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
};
void main()
{
int ch, i;
student s[20];
textbuff b;
cout<<"1.Create\n2.Display\n3.Search\n4.Modify\n5.Exit\n";
do
{
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no. of records: ";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"Enter USN, name, branch and sem of student "<<i+1<<endl;
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;
}
b.pack(s);
break;
case 2: b.unpack(s);
b.display(s);
break;
case 3: b.unpack(s);
b.search(s);
break;
case 4: b.unpack(s);
b.modify(s);
break;
case 5: remove("first.txt");
break;
}
}while(ch != 5);
}
=======================================
=======================================
PROGRAM -4
#include<stdio.h>
#include<fstream.h>
#include<string.h>
int count=1;
class student
{
public:
char name[20],usn[20],branch[20],sem[20],buffer[20];
int array[20];
void input()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the usn\n";
cin>>usn;
cout<<"enter the branch\n";
cin>>branch;
cout<<"enter the sem\n";
cin>>sem;
}
void pack()
{
strcpy(buffer,name);strcat(buffer,"|");
strcat(buffer,usn);strcat(buffer,"|");
strcat(buffer,branch);strcat(buffer,"|");
strcat(buffer,sem);strcat(buffer,"|");
int length=strlen(buffer);
array[count++]=length;
fstream file;
file.open("first.txt",ios::app);
file<<buffer;
file.close();
}
void search_rrn()
{
int rrn,size=0;
char name1[20],usn1[20],branch1[20],sem1[20];
cout<<"enter the rrn:"; cin>>rrn;
if(rrn>=count)
cout<<"no record found at this rrn\n";
else
{
for(int i=1;i<rrn;i++)
size+=array[i];
fstream file;
file.open("first.txt",ios::in);
file.seekg(size,ios::beg);
file.getline(name1,15,'|');
file.getline(usn1,15,'|');
file.getline(branch1,15,'|');
file.getline(sem1,15,'|');
cout<<"name:"<<name1;
cout<<"\n usn:"<<usn1;
cout<<"\n branch:"<<branch1;
cout<<"\n sem:"<<sem1;
cout<<"\nthe rrn:"<<rrn<<endl;
file.close();
}
}
};
void main()
{
int ch;
student s;
do
{
cout<<"\n1.insert 2.search 3.exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:s.input();
s.pack();
break;
case 2:s.search_rrn();
break;
case 3://remove("first.txt");
break;
}
}while(ch!=3);
}
#include<iostream.h>
#include<fstream.h>
#include<string.h>
void swap (char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}
void reverse(char *name)
{
int i,j,size;
size=strlen(name);
for(i=0,j=size-1;i<j;i++,j--)
swap(name[i],name[j]);
}
void main(int argc,char *argv[])
{
char name[30];
if(argc==1)
{
do
{
cout<<"enter name\n";
cin>>name;
reverse(name);
cout<<name;
}
while(!cin.eof());
}
else if(argc==3)
{
fstream ip,op;
ip.open(argv[1],ios::in);
op.open(argv[2],ios::out|ios::app);
do
{
ip>>name;
reverse(name);
cout<<name<<endl;
op<<name;
if(ip.eof())
break;
}
while(1);
}
else
cout<<"error";
}
// ctrl-c to exit !!!
Execution of file -> 1.CPP
1.start ->type cmd .
2. In commad Prompt type -> d :-> cd debug ->
3.Create 2 files 1.txt(Enter some names ) and 2.txt (keep it empty )
4. After executing program there will be a file called 1.exe in debug folder.
5. Type in 1.exe 1.txt 2.txt ( Reversed names will be found in 2.txt )
========================================
========================================
PROGRAM-2
Write a C++ program to read and write student objects with fixedlength
records and the fields delimited by “|”. Implement pack ( ),
unpack ( ), modify ( ) and search ( ) methods.
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdio.h>
class FLB;
class person
{
char usn[30],name[30],address[30],branch[30],college[30];
public:
void input();
void output();
void search(char *fname);
void modify(char *fname);
friend class FLB;
};
class FLB
{
char buff[160];
public:
FLB()
{
for(int i=0;i<160;i++)
buff[i]='\0';
}
void pack(person &p);
void unpack(person &p);
void read(fstream &fs);
void write(char *fname);
};
void person::input()
{
cout<<"enter usn,name,address,branch and college\n";
cin>>usn>>name>>address>>branch>>college;
}
void person::output()
{
cout<<"usn:"<<usn<<"\nname:"<<name<<"\naddress:"<<address<<"\nbranch:"<<branch<<"\ncollege:"<<college;
}
void FLB::pack(person &p)
{
strcpy(buff,p.usn);strcat(buff,"|");
strcat(buff,p.name);strcat(buff,"|");
strcat(buff,p.address);strcat(buff,"|");
strcat(buff,p.branch);strcat(buff,"|");
strcat(buff,p.college);
}
void FLB::unpack(person &p)
{
char *ptr=buff;
while(*ptr)
{
if(*ptr=='|')
*ptr='\0';
ptr++;
}
ptr=buff;
strcpy(p.usn,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.name,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.address,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.branch,ptr);
ptr+=strlen(ptr)+1;
strcpy(p.college,ptr);
}
void FLB::read(fstream &fs)
{
fs.read(buff,sizeof(buff));
}
void FLB::write(char *fname)
{
fstream os(fname,ios::app);
os.write(buff,sizeof(buff));
os.close();
}
void person::search(char *fname)
{
int found=0;
person p;
FLB b;
char key[30];
fstream is(fname,ios::in);
cout<<"enter the usn to be searched";
cin>>key;
while((!is.eof())&&(!found))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if(strcmp(p.usn,key)==0)
{
cout<<"record found\n";
p.output();
found=1;
}
}
if(!found)
cout<<"does not exists\n";
is.close();
}
void person::modify(char *fname)
{
int found=0;
person p;
FLB b;
char key[30],tname[]="temp.txt";
fstream is(fname,ios::in);
fstream tfile(tname,ios::out|ios::app);
cout<<"enter the usn to be searched";
cin>>key;
while((!is.eof()))
{
b.read(is);
if(is.eof())
break;
b.unpack(p);
if((strcmp(p.usn,key)==0)&&(!found))
{
cout<<"record found\n";
p.input();
found=1;
}
b.pack(p);
b.write(tname);
}
if(!found)
cout<<"does not exists\n";
is.close();
tfile.close();
//remove(fname);
//rename(tname,fname);
}
void main()
{
person p;
FLB b;
int ch;
char fname[]="prg2.txt";
do
{
cout<<"1.insert\t 2.search\t 3.modify\t 4.exit\n enter ur choice\n";
cin>>ch;
switch(ch)
{
case 1: p.input();
b.pack(p);
b.write(fname);
break;
case 2: p.search(fname);
break;
case 3: p.modify(fname);
break;
case 4: //remove(fname);
break;
}
}while(ch!=4);
=========================================
=========================================
PROGRAM -3
#include<fstream.h>
#include<stdio.h>
#include<string.h>
int n, no_of_rec, length, len[20];
class student
{
public:
char usn[20], sem[20], name[20], branch[20];
};
class textbuff
{
public:
char buff[500];
void pack(student s[])
{
fstream file1("first.txt", ios::out | ios::app);
clear_buff();
for(int i = 0; i < n; i++)
{
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
}
length = strlen(buff);
file1<<buff;
file1.close();
}
void unpack(student s[])
{
fstream file2("first.txt", ios::in);
clear_buff();
for(int i = 0; i < no_of_rec; i++)
{
file2.getline(s[i].usn, 20, '|');
file2.getline(s[i].name, 20, '|');
file2.getline(s[i].branch, 20, '|');
file2.getline(s[i].sem, 20, '|');
}
}
void clear_buff()
{
for(int i = 0; i < 500; i++)
buff[i] = NULL;
}
void display(student s[])
{
char ch;
if(no_of_rec != 0)
{
cout<<"USN\tName\tBranch\tSem\n";
for(int i = 0; i < no_of_rec; i++)
{
ch = s[i].usn[0];
if(ch != '*')
cout<<s[i].usn<<"\t"<<s[i].name<<"\t"<<s[i].branch<<"\t"<<s[i].sem<<endl;
}
}
else
cout<<"No record found\n";
}
void search(student s[])
{
int flag = 0;
char key[20];
clear_buff();
cout<<"Enter the USN for search: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
cout<<"Record found!!\n";
cout<<"USN: "<<s[i].usn<<", Name: "<<s[i].name<<", Branch: "<<s[i].branch<<", Sem: "<<s[i].sem<<endl;
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
void modify(student s[])
{
char key[20], cvar[20];
/*int len1[20];*/
int flag = 0, len_field;
/*for(int i = 1; i <= no_of_rec; i++)
len1[i] = len[i] - len[i-1];*/
cout<<"Enter the USN of record to be modified: ";
cin>>key;
for(int i = 0; i < no_of_rec; i++)
{
if(strcmp(key, s[i].usn) == 0)
{
fstream file3("first.txt", ios::in | ios::out);
fstream file4("first.txt", ios::out | ios::app);
file3.seekg(len[i], ios::beg);
file3.getline(cvar, 20, '|');
len_field = strlen(cvar); //length of 1 field
file3.seekp(len[i], ios::beg);
for(int j = 0; j < len_field; j++)
file3<<'*';
file3.close();
cout<<"Enter new data:\nEnter USN, name, branch and sem:\n";
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;
clear_buff();
strcat(buff, s[i].usn);
strcat(buff, "|");
strcat(buff, s[i].name);
strcat(buff, "|");
strcat(buff, s[i].branch);
strcat(buff, "|");
strcat(buff, s[i].sem);
strcat(buff, "|");
no_of_rec++;
len[no_of_rec] = strlen(buff) + length;
// file3.seekp(0, ios::end);
file4<<buff;
file4.close();
flag = 1;
}
}
if(!flag)
cout<<"No such USN found\n";
}
};
void main()
{
int ch, i;
student s[20];
textbuff b;
cout<<"1.Create\n2.Display\n3.Search\n4.Modify\n5.Exit\n";
do
{
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no. of records: ";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"Enter USN, name, branch and sem of student "<<i+1<<endl;
cin>>s[i].usn>>s[i].name>>s[i].branch>>s[i].sem;
}
b.pack(s);
break;
case 2: b.unpack(s);
b.display(s);
break;
case 3: b.unpack(s);
b.search(s);
break;
case 4: b.unpack(s);
b.modify(s);
break;
case 5: remove("first.txt");
break;
}
}while(ch != 5);
}
=======================================
=======================================
PROGRAM -4
#include<stdio.h>
#include<fstream.h>
#include<string.h>
int count=1;
class student
{
public:
char name[20],usn[20],branch[20],sem[20],buffer[20];
int array[20];
void input()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the usn\n";
cin>>usn;
cout<<"enter the branch\n";
cin>>branch;
cout<<"enter the sem\n";
cin>>sem;
}
void pack()
{
strcpy(buffer,name);strcat(buffer,"|");
strcat(buffer,usn);strcat(buffer,"|");
strcat(buffer,branch);strcat(buffer,"|");
strcat(buffer,sem);strcat(buffer,"|");
int length=strlen(buffer);
array[count++]=length;
fstream file;
file.open("first.txt",ios::app);
file<<buffer;
file.close();
}
void search_rrn()
{
int rrn,size=0;
char name1[20],usn1[20],branch1[20],sem1[20];
cout<<"enter the rrn:"; cin>>rrn;
if(rrn>=count)
cout<<"no record found at this rrn\n";
else
{
for(int i=1;i<rrn;i++)
size+=array[i];
fstream file;
file.open("first.txt",ios::in);
file.seekg(size,ios::beg);
file.getline(name1,15,'|');
file.getline(usn1,15,'|');
file.getline(branch1,15,'|');
file.getline(sem1,15,'|');
cout<<"name:"<<name1;
cout<<"\n usn:"<<usn1;
cout<<"\n branch:"<<branch1;
cout<<"\n sem:"<<sem1;
cout<<"\nthe rrn:"<<rrn<<endl;
file.close();
}
}
};
void main()
{
int ch;
student s;
do
{
cout<<"\n1.insert 2.search 3.exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:s.input();
s.pack();
break;
case 2:s.search_rrn();
break;
case 3://remove("first.txt");
break;
}
}while(ch!=3);
}
=====================================
=====================================
=====================================
PROGRAM-5
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
char index[100][50],indexadd[100][50];
int nor;
void smi()
{
char temp[100],temp1[100];
for(int i=0;i<nor;i++)
{
for(int j=i+1;j<nor;j++)
{
if(strcmp(index[i],index[j])>0)
{
strcpy(temp,index[i]);
strcmp(index[i],index[j]);
strcmp(index[j],temp);
strcmp(temp1,indexadd[i]);
strcmp(indexadd[i],indexadd[j]);
strcmp(indexadd[j],temp1);
}
}
}
}
class stud
{
public:char usn[10],name[20],brn[10],sem[10];
void unpack(int);
void add();
void del();
void search();
void disp();
};
void stud::add()
{
char buf[100],sadd[50];
cout<<"\n enter usn,name,branch,and sem:\n";
cin>>usn>>name>>brn>>sem;
for(int i=0;i<nor;i++)
{
if(strcmp(usn,index[i])==0)
{
cout<<"\nduplicate entry\n";
return;
}
}
strcpy(buf,usn);
strcat(buf,"|");
strcat(buf,name);
strcat(buf,"|");
strcat(buf,brn);
strcat(buf,"|");
strcat(buf,sem);
strcat(buf,"|");
fstream f1("index.txt",ios::app);
fstream f2("rec.txt",ios::app);
f2.seekp(0,ios::end);
strcpy(index[nor],usn);
itoa(f2.tellp(),sadd,10);
strcpy(indexadd[nor],sadd);
f1<<usn<<'|'<<f2.tellp()<<'\n';
f2<<buf;
nor++;
smi();
f1.close();
f2.close();
}
void stud::search()
{
char usn[10];
stud s;
cout<<"\nenter usn for search:";
cin>>usn;
int max=nor-1,min=0,flag=0,add,mid;
while(min<=max&&flag==0)
{
mid=(min+max)/2;
if(strcmp(usn,index[mid])==0)
{
flag=1;
add=atoi(indexadd[mid]);
s.unpack(add);
cout<<"record found\n";
cout<<"usn:"<<s.usn<<"name:"<<s.name<<"branch:"<<s.brn<<"sem:"<<s.sem<<endl;
}
else
if(strcmp(usn,index[mid])>0)
min=mid+1;
else
max=mid-1;
}
if(!flag)
cout<<"\nsearch failed!!\n";
}
void stud::unpack(int add)
{
fstream f1("rec.txt",ios::in);
f1.seekg(add);
f1.getline(usn,10,'|');
f1.getline(name,10,'|');
f1.getline(brn,10,'|');
f1.getline(sem,10,'|');
f1.close();
}
void stud::del()
{
char usn[10];
fstream f1("index.txt",ios::out|ios::trunc);
cout<<"\nenter usn to delete:";
cin>>usn;
int max=nor-1,min=0,flag=0,mid;
while(min<=max&&flag==0)
{
mid=(min+max)/2;
if(strcmp(usn,index[mid])==0)
{
flag=1;
cout<<"\nrecord delated\n";
for(int i=mid;i<nor;i++)
{
strcpy(index[i],index[i+1]);
strcpy(indexadd[i],indexadd[i+1]);
}
nor--;
}
else
if(strcmp(usn,index[mid])>0)
min=mid+1;
else
max=min-1;
}
for(int i=0;i<nor;i++)
{
f1.write(index[i],strlen(index[i]));
f1.write("|",1);
f1.write(indexadd[i],strlen(indexadd[i]));
f1.write("|\n",2);
}
f1.close();
if(!flag)
cout<<"\ndeletion failed\n";
}
void stud::disp()
{
stud s;
int add;
cout<<"usn\tname\tbranch\tsem\n";
for(int i=0;i<nor;i++)
{
add=atoi(indexadd[i]);
s.unpack(add);
cout<<s.usn<<"\t"<<s.name<<"\t"<<s.brn<<"\t"<<s.sem<<endl;
}
}
void main()
{
stud s;
int ch;
cout<<"1 add\n2 search\n3 delete\n4 display\n5 exit";
do
{
cout<<"enter your chaice:";
cin>>ch;
switch(ch)
{
case 1:s.add();
break;
case 2:s.search();
break;
case 3:s.del();
break;
case 4:s.disp();
break;
case 5:remove("index.txt");
remove("rec.txt");
break;
}
}while(ch!=5);
}
=======================================
=======================================
=======================================
PROGRAM -6
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
class record
{
public:
char usn[10],name[20],branch[10],sem[2];
}rec[20],found[20];
int m=0,n;
void add()
{
int i;
cout<<"Enter number of students: ";
cin>>n;
cout<<"Enter Name, USN, Branch and Sem\n";
n=m+n;
for(i=m;i<n;i++)
{
cout<<"Student "<<i+1<<" :\n";
cin>>rec[i].name>>rec[i].usn>>rec[i].branch>>rec[i].sem;
m++;
}
}
void sort_records()
{
int i,j;
record temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(rec[j].name,rec[j+1].name)>0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
else if(strcmp(rec[j].name,rec[j+1].name)==0)
{
if(strcmp(rec[j].usn,rec[j+1].usn)>0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}
}
}
}
void create_file()
{
fstream index("secindex.txt",ios::out);
fstream file("record.txt",ios::out);
for(int i=0;i<n;i++)
{
index<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
file<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].branch<<"|"<<rec[i].sem<<"\n";
}
index.close();
file.close();
}
void retrieve_record(char *index)
{
fstream f1("record.txt",ios::in);
int i;
char ind[3],usn[10],name[20],branch[10],sem[2];
for(i=0;i<n;i++)
{
f1.getline(ind,3,'|');
f1.getline(usn,10,'|');
f1.getline(name,20,'|');
f1.getline(branch,10,'|');
f1.getline(sem,2,'\n');
if(strcmp(index,ind)==0)
cout<<"USN: "<<usn<<" Name: "<<name<<" Branch: "<<branch<<" Sem: "<<sem<<endl;
f1.seekg(2,ios::cur);
}
f1.close();
}
void search()
{
int k=0,i,flag;
fstream f1("secindex.txt",ios::in);
char name[20],usn[10],ind[3],key_usn[10],key_name[20];
char index[20][20];
cout<<"Enter the name for search: ";
cin>>key_name;
for(i=0;i<n;i++)
{
f1.getline(name,20,'|');
f1.getline(usn,10,'|');
f1.getline(ind,3,'\n');
if(strcmp(key_name,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
f1.close();
if(k==0)
cout<<"Search failed!!\n";
else if(k==1)
retrieve_record(index[0]);
else
{
cout<<"Choose USN from the list:\n";
for(i=0;i<k;i++)
cout<<found[i].usn<<endl;
cin>>key_usn;
flag=0;
for(i=0;i<k;i++)
{
if(strcmp(key_usn,found[i].usn)==0)
{
retrieve_record(index[i]);
flag=1;
}
}
if(!flag)
cout<<"Invalid entry!!\n";
}
}
void delete_record(char *indx)
{
int i,flag;
char ch;
fstream f1("record.txt",ios::in);
char index[20][20];
for(i=0;i<n;i++)
{
f1.getline(index[i],3,'|');
f1.getline(rec[i].usn,10,'|');
f1.getline(rec[i].name,20,'|');
f1.getline(rec[i].branch,10,'|');
f1.getline(rec[i].sem,2,'\n');
/* cout<<f1.tellg()<<endl;
f1>>ch;
cout<<ch<<endl;
f1>>ch;
cout<<ch<<endl;*/
f1.seekg(2,ios::cur);
cout<<f1.tellg()<<endl;
}
flag=-1;
for(i=0;i<n;i++)
if(strcmp(index[i],indx)==0)
flag=i;
if(flag!=n-1)
{
for(i=flag;i<n;i++)
rec[i]=rec[i+1];
}
n--;
cout<<"Record deleted!!\n";
f1.close();
f1.open("secindex.txt",ios::out);
fstream f2("record.txt",ios::out);
for(i=0;i<n;i++)
{
f1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
f2<<i<<"|"<<rec[i].usn<<"|"<<rec[i].name<<"|"<<rec[i].branch<<"|"<<rec[i].sem<<"\n";
}
f1.close();
f2.close();
}
void del()
{
int k=0,i,flag;
fstream f1("secindex.txt",ios::in);
char name[20],usn[10],ind[3],key_usn[10],key_name[20];
char index[20][20];
cout<<"Enter the name to delete: ";
cin>>key_name;
for(i=0;i<n;i++)
{
f1.getline(name,20,'|');
f1.getline(usn,10,'|');
f1.getline(ind,3,'\n');
if(strcmp(key_name,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
f1.close();
if(k==0)
cout<<"Deletion failed!!\n";
else if(k==1)
delete_record(index[0]);
else
{
cout<<"Choose USN from the list:\n";
for(i=0;i<k;i++)
cout<<found[i].usn<<endl;
cin>>key_usn;
flag=0;
for(i=0;i<k;i++)
{
if(strcmp(key_usn,found[i].usn)==0)
{
delete_record(index[i]);
flag=1;
}
}
if(!flag)
cout<<"Invalid entry!!\n";
}
}
void main()
{
int ch;
cout<<"1:Add\n2:Search\n3:Delete\n4:Exit\n";
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
add();
sort_records();
create_file();
break;
case 2:
search();
break;
case 3:
del();
break;
case 4:
remove("secindex.txt");
remove("record.txt");
break;
}
}while(ch!=4);
}
No comments:
Post a Comment