/*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 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,’n’);
count++;
if(atoi(usn)==atoi(reg))
{
cout<<”nrecord found”<<count<<”positionn”;
readrec();
pack();
write(file1);
cout<<”nrecord modified.n”;
return;
}
else if(strlen(name)==0)
break;
pack();
write(file1);
}
cout<<”nrecord not foundn”;
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,’n’);
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,’n’);
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,”n”);
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 recordn”;
cout<<”3:modify recordt4:search recordn”;
cout<<”press 5 to exitn”;
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
prithu.a.roy says
hi all
the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by n ie newline
so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #
in short we have to replace n by # in the second programme
prithu.a.roy says
hi all
the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by n ie newline
so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #
in short we have to replace n by # in the second programme
prade23 says
hiiii…..i needed all the FS programs urgently
Siddivinayak676 says
hello sir,
can these programs be executed using fedora
prithu.a.roy says
hi all
the third program is literally same as the second one only difference is in second program we are storing each record of student in one line hence length of line is fixed and program determines it by n ie newline
so for the third program we have variable length record hence the record should be differentiated from each other by some diff delimiter i have taken #
in short we have to replace n by # in the second programme