Enter your email address & Win:

File Structure ( FS ) Lab Program 2 ( Set 2 )

// March 30th, 2009 // Educational, Engineering, File Structure (F.S.) Lab // Written by sandeephegde

-->

/*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] = ‘\0′;
cout<<”Name: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘\0′;
cout<<”Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘\0′;
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] = ‘\0′;

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] = ‘\0′;
cout<<”Semester: “<<field<<endl;
p=0,q++;
for (;buf[q]!= ‘|’; q++)
field[p++] = buf[q];
field[p] = ‘\0′;
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] = ‘\0′;
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. Insert\n2.  Display all\n3.  Search\n4.  Modify\n5.  Exit\n”;
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


If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. File Structure ( FS ) Lab Program 2 ( 6th Semester Information Science)
  2. File Structure ( FS ) Lab Program 3 ( 6th Semester Information Science)
  3. File Structure ( FS ) Lab Program 4 ( 6th Semester Information Science)
  4. File Structure ( FS ) Lab Program 12 ( 6th Semester Information Science)
  5. File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science) – Alternative 2
blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes