This post was published long ago, when I was a student and an amateur blogger. The links might be broken and content may not be useful anymore. Please read this content keeping its age in mind.
/c++ programme to read and write students object with fixed length records and
fields delimeted by’|’Implemend pack(),UNPACK(),modify()and search() methods/
#include<iostream.h>
#include<process.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
class Student
{
public:
char buf[40],name[10],semes[10],branch[10];
void unpack(ifstream & ifile);
void pack(ofstream & ofile);
void search(ifstream & ifile, char key[]);
void modify(fstream & iofile, char key[]);
};
void Student::pack(ofstream & ofile)
{
clrscr();
strcpy(buf,””);
cout<<“Name “<<endl;
cin>>name;
strcat(buf,name);
strcat(buf,”|”);
cout<<“Semester “<<endl;
cin>>semes;
strcat(buf,semes);
strcat(buf,”|”);
cout<<“Branch “<<endl;
cin>>branch;
strcat(buf,branch);
strcat(buf,”|”);
while(strlen(buf)<40)
{
strcat(buf,”.”);
}
strcat(buf,”n”);
ofile.write(buf,strlen(buf));
}
void Student::unpack(ifstream & ifile)
{
char *field;
clrscr();
while(1)
{
strcpy(buf,””);
ifile.getline(buf,42,’n’);
if(ifile.fail())
break;
int p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
cout<<“Name: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
cout<<“Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
cout<<“Branch: “<<field<<endl<<endl;
}
}
void Student::search(ifstream & ifile, char key[])
{
char field[10];
int flag = 0;
clrscr();
while(1)
{
strcpy(buf,””);
ifile.getline(buf,42,’n’);
if(ifile.fail())
break;
int p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
if(strcmp(field,key)==0)
{
cout<<“Record found and details are:”<<endl;
cout<<“Name: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
cout<<“Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
cout<<“Branch: “<<field<<endl;
flag=1;
break;
}
}
if(!flag)
cout<<“Record with given key not found”<<endl;
}
void Student::modify(fstream & iofile, char key[])
{
char field[10];
// int flag = 0;
clrscr();
while(1)
{
strcpy(buf,””);
iofile.getline(buf,42,’n’);
if(iofile.fail())
break;
int p=0,q;
for (q = 0;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ”;
if(strcmp(field,key)==0)
{
clrscr();
cout<<“Record found Enter modification details”<<endl;
iofile.seekp(-42,ios::cur);
//pack(iofile);
strcpy(buf,””);
cout<<“Name “<<endl;
cin>>name;
strcat(buf,name);
strcat(buf,”|”);
cout<<“Semester “<<endl;
cin>>semes;
strcat(buf,semes);
strcat(buf,”|”);
cout<<“Branch “<<endl;
cin>>branch;
strcat(buf,branch);
strcat(buf,”|”);
while(strlen(buf)<40)
{
strcat(buf,”.”);
}
strcat(buf,”n”);
iofile.write(buf,strlen(buf));
break;
}
}
}
void main()
{
int n,i=0,ch;
Student stu;
clrscr();
for(;;)
{
clrscr();
cout<<“1. Insertn2. Display alln3. Searchn4. Modifyn5. Exitn”;
cout<<“Enter your choice”<<endl;
cin>>ch;
switch(ch)
{
case 1: ofstream ofile;
ofile.open(“student.txt”,ios::out|ios::trunc);
cout<<“enter the no. of students”<<endl;
cin>>n;
while(i<n)
{
stu.pack(ofile);
i++;
}
ofile.close();
break;
case 2: ifstream infile;
infile.open(“student.txt”,ios::in);
stu.unpack(infile);
getch();
infile.close();
break;
case 3: cout<<“Enter the record name to be searched”<<endl;
char key[10];
cin>>key;
ifstream ifile;
ifile.open(“student.txt”,ios::in);
stu.search(ifile,key);
getch();
ifile.close();
break;
case 4: fstream iofile;
iofile.open(“student.txt”,ios::in|ios::out);
cout<<“Enter the record name to be modified”<<endl;
cin>>key;
stu.modify(iofile,key);
getch();
iofile.close();
break;
default: exit(0);
}
}
}
Credits : Sahana V
Leave a ReplyCancel reply