wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
10.Write a C++ program to read and write student objects with Variable -
Length records using any suitable record structure. Implement pack ( ),
unpack ( ), modify ( ) and search ( ) methods.
#include..stdio.h>
#include..stdlib.h>
#include..iostream.h>
#include..fstream.h>
#include..conio.h>
#include..string.h>
#include..iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\n Enter the student name = "; cin>>s.name;
cout..."\n Enter the usn = "; cin>>s.usn;
cout..."\n Enter the age = "; cin>>s.age;
cout..."\n Enter the sem = "; cin>>s.sem;
cout..."\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);strcat(s.buffer,"\n");
app...s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\n');
if(strcmp(s.usn,usn)==0)
{
cout..."\nRecord found";
cout..."\n"...s.name..."\t"...s.usn..."\t"...s.age..."\t"...s.sem..."\t"...s.branch;
getch();
return;
}
}
cout..."\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
i++;
}
i--;
for(j=0;j..i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout..."\nThe old values of the record with usn "<
cout..."\nusn = "... s1[j].usn;
cout..."\nage = "... s1[j].age;
cout..."\nsem = "... s1[j].sem;
cout..."\nbranch = "... s1[j].branch;
cout..."\nEnter the new values \n";
cout..."\nname = "; cin>>s1[j].name;
cout..."\nusn = "; cin>>s1[j].usn;
cout..."\nage = "; cin>>s1[j].age;
cout..."\nsem = "; cin>>s1[j].sem;
cout<<"\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout..."\n Record with usn "<
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout..."\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j..i;j++)
{
out1...s1[j].name...'|'...s1[j].usn...'|'...s1[j].age...'|'...s1[j].sem...'|'...s1[j].sem...'|'...s1[j].branch...'\n';
}
out1.close();
}
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout..."\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout..."\n O:exit\n 1: write to file\n 2:display the file"
..."\n 3:modify the file\n 4:search";
cout..."\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout..."\nInvalid input....";break;
}
}
}
No comments:
Post a Comment