File Structure ( FS ) Lab Program 3 ( 6th Semester Information Science)
// March 31st, 2009 // Educational, Engineering, File Structure (F.S.) Lab // Written by sandeephegde
-->/*c++ program to read and write students object with variable length records and
fields delimited by’|’Implemend pack(),UNPACK(),modify()and search() methods*/
#include iostream.h
#include conio.h
#include fstream.h
#include string.h
#include process.h
#include stdlib.h
#include stdio.h
class student
{
public:
char usn[100],name[100],address[100],branch[100],college[100],buffer[100];
char sem[100];
void readrec();
void pack();
void write(fstream&file);
void unpack(fstream&file);
void search(fstream&file);
void modify(fstream&file);
};
void student::modify(fstream&file)
{
char reg[80];
int count=0;
file.seekg(0);
fstream file1;
file1.open(”data2.txt”,ios::out);
cout<>reg;
while(1)
{
file.getline(usn,80,’|’);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’#’);
count++;
if(atoi(usn)==atoi(reg))
{
cout<<”\nrecord found”<<count<<”position\n”;
readrec();
pack();
write(file1);
cout<<”\nrecord modified.\n”;
return;
}
else if(strlen(name)==0)
break;
pack();
write(file1);
}
cout<<”\nrecord not found\n”;
file1.close();
}
void student::search(fstream&file)
{
char reg[80],temp[80];
int count=0;
file.seekg(0);
cout<>reg;
while(1)
{
file.getline(usn,80,’|’);
strcpy(temp,usn);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’#’);
count++;
if(atoi(temp)==atoi(reg))
{
cout<<”\nRecord found at”<<count<<”position”;
return;
}
if(strlen(usn)==0)
break;
}
cout<<”\nRecord not found”;
}
void student::unpack(fstream&file)
{
while(1)
{
file.getline(usn,80,’|’);
file.getline(name,80,’|’);
file.getline(address,80,’|’);
file.getline(sem,80,’|’);
file.getline(branch,80,’|’);
file.getline(college,80,’#’);
if(strlen(usn)==0)
break;
cout<<flush<<”\nusn:”<<usn<<endl;
cout<<”\nname:”<<name<<endl
<<”\naddress:”<<address<<endl
<<”\nsemister:”<<sem<<endl
<<”\nBranch:”<<branch<<endl
<<”\ncollege:”<<college<<endl;
cout<<”\n**************\n”;
}
}
void student::pack()
{
char tempbuf[100];
tempbuf[0]=”;
strcat(tempbuf,usn);
strcat(tempbuf,”|”);
strcat(tempbuf,name);
strcat(tempbuf,”|”);
strcat(tempbuf,address);
strcat(tempbuf,”|”);
strcat(tempbuf,sem);
strcat(tempbuf,”|”);
strcat(tempbuf,branch);
strcat(tempbuf,”|”);
strcat(tempbuf,college);
strcat(tempbuf,”#”);
strcpy(buffer,tempbuf);
}
void student::write(fstream&file)
{
file<<buffer;
}
void student::readrec()
{
cout<>usn;
cout<>name;
cout<>address;
cout<>sem;
cout<>branch;
cout<>college;
}
void main()
{
char filename[25];
int choice;
fstream file;
student st;
clrscr();
cout<>filename;
while(1)
{
cout<<”\n*******program|menue********\n”;
cout<<”1:pack Record \t2:unpack record\n”;
cout<<”3:modify record\t4:search record\n”;
cout<<”press 5 to exit\n”;
cout<<”****************************\n”;
cout<>choice;
switch(choice)
{
case 1:st.readrec();
st.pack();
file.open(filename,ios::out|ios::app|ios::in);st.write(file);
file.close();
break;
case 2:file.open(filename,ios::out|ios::app|ios::in);
st.unpack(file);
file.close();
break;
case 3:file.open(filename,ios::out|ios::app|ios::in);
st.modify(file);
file.close();
unlink(filename);
rename(”data2.txt”,filename);
break;
case 4:file.open(filename,ios::out|ios::app|ios::in);
st.search(file);
file.close();
break;
case 5:exit(0);
}
}
}
Credits : Prithu A Roy
If you enjoyed this post, make sure you subscribe to my RSS feed!
Related posts:
- File Structure ( FS ) Lab Program 2 ( 6th Semester Information Science)
- File Structure ( FS ) Lab Program 4 ( 6th Semester Information Science)
- File Structure ( FS ) Lab Program 11 ( 6th Semester Information Science)
- File Structure ( FS ) Lab Program 8 ( 6th Semester Information Science)
- File Structure ( FS ) Lab Program 12 ( 6th Semester Information Science)



