Tuesday, March 16, 2010

wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
14. Write a C++ program to implement index on secondary key, the name,
for a file of student objects. Implement add ( ), search ( ), delete ( ) using
the secondary index.

#include..iostream.h>
#include..string.h>
#include..fstream.h>
#include..stdlib.h>

//using namespace std;

class record
{
public: char age[5];
char usn[20],name[20],branch[5]; char sem[2];
}rec[20],found[20];

char st_no[5],rt_name[20];
int no;

void sort_records()
{
int i,j;
record temp;
for(i=0;i..no-1;i++)
for(j=0;j..no-i-1;j++)
if(strcmp(rec[j].name, rec[j+1].name) > 0)
{
temp=rec[j];
rec[j]=rec[j+1];
rec[j+1]=temp;
}
}

void create_indexfile()
{
fstream index,index2;
int i;
index.open("secindex.txt",ios::out);
index2.open("record.txt",ios::out);
for(i=0;i..no;i++)
{
index...rec[i].name..."|"
...rec[i].usn..."|"...i..."\n";
index2...i..."|"...rec[i].usn..."|"...rec[i].name..."|"...rec[i].age..."|"
...rec[i].sem..."|"...rec[i].branch..."\n";
}
}

void retrieve_record(char* index)
{
fstream file;
int i;
char ind[2],usn[20],name[20],age[3],sem[3],branch[10];
file.open("record.txt",ios::in);
for(i=0;i..no;i++)
{
file.getline(ind,4,'|');
file.getline(usn,20,'|');
file.getline(name,20,'|');
file.getline(age,4,'|');
file.getline(sem,4,'|');
file.getline(branch,5,'\n');
if(strcmp(index,ind) == 0)
cout..."\nUSN: "...usn
..."\nName: "...name
..."\nAge: "...age
..."\nSem: "...sem
..."\nBranch: "...branch;
}
file.close();
return;
}

void retrieve_details()
{
int k=0,i;
char name[20],usn[20],ind[2];
char chusn[20];
char index[20][20];
fstream file;
file.open("secindex.txt",ios::in);
for(i=0;i..no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(name,rt_name) == 0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
retrieve_record(index[0]);
return;
}
else
{
cout..."Please choose the candidate's USN: \n";
for(i=0;i..k;i++)
cout..."Name: "...found[i].name..." USN: "...found[i].usn...endl;
}
cin>>chusn;
for(i=0;i..k;i++)
{
if(strcmp(chusn,found[i].usn) == 0)
{
retrieve_record(index[i]);
return;
}
}
cout..."Invalid Entry! \n";
return;
}

void delete_record(char indx[])
{
int i;
fstream file1,file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
char index[20][20];
file2.open("record.txt",ios::in);
for(i=0;i..no;i++)
{
file2.getline(ind,4,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,8,'\n');
strcpy(index[i],ind);
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i..no;i++)
{
if(strcmp(index[i],indx) == 0)
flag=i;
}
if(flag==-1)
{
cout..."Error!\n";
return;
}
if(flag==(no-1))
{
no--;
cout..."Deleted!\n";
return;
}
for(i=flag;i..no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout..."Deleted!\n";
file2.close();
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i..no;i++)
{
file1...rec[i].name..."|"...rec[i].usn..."|"...i..."\n";

file2...i..."|"...rec[i].usn..."|"...rec[i].name..."|"
...rec[i].age..."|"...rec[i].sem..."|"...rec[i].branch..."\n";
}
file1.close();
file2.close();
return;
}


void delete_index(char* nam)
{
fstream file;
int i;
int k=0;
char name[20],usn[20],ind[5],index[20][20],chusn[20];
file.open("secindex.txt",ios::in);
for(i=0;i..no;i++)
{
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(ind,4,'\n');
if(strcmp(nam,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(index[k],ind);
k++;
}
}
file.close();
if(k==1)
{
delete_record(index[0]);
return;
}
else
{
cout..."Please choose the candidate's USN: \n";
for(i=0;i..k;i++)
{
cout..."Name: "...found[i].name..." USN: "...found[i].usn...endl;
}
}
cin>>chusn;
for(i=0;i..k;i++)
{
if(strcmp(chusn,found[i].usn)==0)
{
delete_record(index[i]);
return;
}
}
cout..."Invalid Entry!\n";
return;
}

int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_name[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout..."File creation Error!\n";
exit(0);
}
for(;;)
{
cout..."\nl:Add Record\n 2:Search Record"
..."\n3:Delete Record\n 4:Display Record\n";
cin>>ch;
switch(ch)
{
case 1: cout..."Enter the no. of students : "; cin>>no;
cout..."Enter the details :\n";
for(i=0;i..no;i++)
{
cout..."\nName : "; cin>>rec[i].name;
cout..."Age : "; cin>>rec[i].age;
cout..."USN : "; cin>>rec[i].usn;
cout..."Sem : "; cin>>rec[i].sem;
cout..."Branch : "; cin>>rec[i].branch;
}
sort_records();
create_indexfile();
file1.close ();
file2.close();
break;

case 2: cout..."Enter name of the student whose record is to be displayed\n";
cin>>st_name;
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout..."Error !\n";
exit(0);
}
flag1=0;
for(i=0;i..no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(st_no,4,'\n');

if(strcmp(st_name,rt_name)==0)
{
retrieve_details();
flag1=1;
goto one;
}
}
one: if(!flag1)
cout..."Record search failed !\n";
file1.close();
break;

case 3: cout..."Enter name of student whose record is to be deleted\n";
cin>>st_name;
file1.open("secindex.txt",ios::in);
if(!file1)
{
cout..."Error !\n";
exit(0);
}
flag=0;
for(i=0;i..no;i++)
{
file1.getline(rt_name,20,'|');
file1.getline(rt_usn,20,'|');
file1.getline(ind,4,'\n');

if(strcmp(st_name,rt_name) == 0)
{
delete_index(rt_name);
flag=1;
}
}
if(!flag)
cout..."Deletion failed !\n";
file1.close();
break;

case 4: for(i=0;i..no;i++)
{
cout..."\n\nUSN : "...rec[i].usn
..."\nName: "...rec[i].name
..."\nAge : "...rec[i].age
..."\nSem : "...rec[i].sem
..."\nBranch : "...rec[i].branch..."\n";
}
break;

default: cout..."Invalid option !\n";
exit(0);
break;
}
}
return 0;
}


wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..

13.Write a C++ program to implement simple index on primary key for a
file of student objects. Implement add ( ), search ( ), delete ( ) using the
index.

#include..iostream.h>
#include..string.h>
#include..fstream.h>
#include..stdlib.h>

//Record specification
class record
{
public: char age[5];
char usn[20],name[20],branch[5];
char sem[2];
}rec[20];

char st_no[5];
int no;

void retrieve_details()
{
fstream file2;
char name[20],usn[20],branch[5];
char ind[5],age[5],sem[5];
file2.open("record.txt",ios::in);
for(int i=0;i..no;i++) //Unpacking record data
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
if(strcmp(ind,st_no)==0) //Required record found- so print details
{
cout..."\n\n"..."Student details are: ";
cout..."\n\nUSN: "...usn..."\nName: "...name..."\nAge: "...age..."\nSem: "...sem..."\nBranch: "...branch..."\n";
}
}
file2.close();
}

void delete_record(char usno[])
{
int i;
fstream file1, file2;
char age[5],sem[5],branch[5],usn[20],name[20],ind[5];
file2.open("record.txt",ios::in);
for(i=0;i..no;i++) //Unpack records
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');

strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i..no;i++) //Check for the record's existence
{
if(strcmp(rec[i].usn,usno)==0)
flag=i;
}
if(flag==-1) //Record not found
{
cout..."Error !\n";
return;
}
if(flag==(no-1)) //Delete found record
{
no--;
cout..."Deleted !\n";
return;
}
for(i=flag;i..no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout..."\nDeleted !\n";
file2.close();
file1.open("index.txt",ios::out); //Open index and record files
file2.open("record.txt",ios::out); //After deletion
for(i=0;i..no;i++) //Pack index n record data onto files
{
file1...rec[i].usn..."|"...i..."\n";
file2...i..."|"...rec[i].usn..."|"...rec[i].name..."|"...rec[i].age..."|"...rec[i].sem..."|"...rec[i].branch..."\n";
}
file1.close();
file2.close();
return;
}

int main()
{
fstream file1,file2;
int ch;
char rt_usn[20],st_usn[20];
char ind[2],name[20],age[2],sem[5],branch[5];
int i,flag,flag1;
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout..."File creation Error! \n";
exit(0);
}
for(;;)
{
cout..."\n1: Add Record"
..."\n2: Search Record"
..."\n3: Delete Record"
..."\n4: Display Record";
cin>>ch;
switch(ch)
{
case 1: cout..."Enter the no. of students : "; cin>>no;
cout..."Enter the details:\n";
for(i=0;i..no;i++) //Pack data onto the index and record files
{ //USN is the indexed data
cout..."\nName: "; cin>>rec[i].name;
cout..."Age: "; cin>>rec[i].age;
cout..."USN: "; cin>>rec[i].usn;
cout..."Sem: "; cin>>rec[i].sem;
cout..."Branch: "; cin>>rec[i].branch;

file1...rec[i].usn..."|"...i..."\n";
file2...i..."|"...rec[i].usn..."|"...rec[i].name..."|"...rec[i].age..."|"...rec[i].sem..."|"...rec[i].branch..."\n";
}
file1.close();
file2.close();
break;
case 2: cout..."Enter USN whose record is to be displayed: ";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout..."\nError !\n";
exit(0);
}
flag1=0;
for(i=0;i..no;i++)
{
file1.getline(rt_usn,20,'|'); //Unpack index file and
file1.getline(st_no,4,'\n'); //look for a match in the USN
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details(); //Retrieve details if index found
flag1=1;
}
}
if(!flag1)
cout..."Record search failed!\n";
file1.close();
break;

case 3: cout..."Enter USN whose record is to be deleted: ";
cin>>st_usn;
file1.open("index.txt", ios::in);
if(!file1)
{
cout..."Error! \n";
exit(0);
}
flag=0;
for(i=0;i..no;i++)
{
file1.getline(rt_usn,20,'|'); //Search index file and
file1.getline(st_no,4,'\n'); //call del if index found
if(strcmp(st_usn, rt_usn)==0)
{
delete_record(rt_usn);
flag=1;
}
}
if(!flag)
cout..."Deletion Failed\n";
file1.close();
break;
case 4: for(i=0;i..no;i++)
{
cout..."\n\n USN: "...rec[i].usn
..."\n Name: "...rec[i].name
..."\n Age: "...rec[i].age
..."\n Sem: "...rec[i].sem
..."\n Branch: "...rec[i].branch;
}
break;
default: cout..."Invalid choice";
exit(0);
break;
}
}
}

wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
12. Write a C++ program to write student objects with Variable - Length
records using any suitable record structure and to read from this file a
student record using RRN.

#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];
};

void search()
{
char usn[15];
int i=0;
student s1[100];
cout..."\nEnter the usn to be searched: "; cin>>usn;
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\tUsn\tAge\tSem\tBranch\n");
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++;
}
for(int j=0; j..i-1; j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
printf("\n Record found");
printf("\n%s\t%s\t%s\t%s\t%s",s1[j].name,s1[j].usn,s1[j].age,s1[j].sem,s1[j].branch);
return;
}
}
cout..."\nRecord not found";
return;
}

void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app);
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\nEnter the student name: "; cin>>s.name;
cout..."\nEnter the usn: "; cin>>s.usn;
cout..."\nEnter the age: "; cin>>s.age;
cout..."\nEnter the sem: "; cin>>s.sem;
cout..."\nEnter the branch: "; cin>>s.branch;

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;
app.close();
}


void main()
{
clrscr();
int ch;
fstream out;
out.open("student.txt",ios::out);
if(!out)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
cout..."\nO:exit"
..."\n1:Insert"
..."\n2:Search"
..."\nEnter the choice = "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: search();break;
case 0: exit(0);
default: cout..."\nInvalid option";
}
}
}
wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
11. 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 "...usn..." are ";
cout..."\nname = "... s1[j].name;
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 "...usn..." is not present";
getch();
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;
}
}
}

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..."\nname = "... s1[j].name;
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 "< getch();
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;
}
}
}

Monday, March 15, 2010

wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
9.Write a C++ program to read and write student objects with fixed-length
records and the fields delimited by “|”. 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[45];
};

student s2[100];

void writeRecord() //Function to add record to file
{
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);

int count=strlen(s.buffer);
for(int k=0;k..45-count;k++)
strcat(s.buffer,"!");
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,'|');
in.getline(extra,45,'\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;
char extra[45];
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,'!');
in.getline(extra,45,'\n');
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');
in.getline(extra,45,'\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..."\nname = "... s1[j].name;
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 "...usn..." is not present";
getch();
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++)
{
strcpy(buffer,s1[j].name); strcat(buffer,"|");
strcat(buffer,s1[j].usn); strcat(buffer,"|");
strcat(buffer,s1[j].age); strcat(buffer,"|");
strcat(buffer,s1[j].sem); strcat(buffer,"|");
strcat(buffer,s1[j].branch);

int count=strlen(buffer);
for(int k=0;k..45-count;k++)
strcat(buffer,"!");

strcat(buffer,"\n");
out1...buffer;
}
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;
}
}
}

wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
8. Write a C++ program to read series of names, one per line, from standard
input and write these names spelled in reverse order to the standard
output using I/O redirection and pipes. Repeat the exercise using an
input file specified by the user instead of the standard input and using an
output file specified by the user instead of the standard output.


#include..fstream.h>
#include..iostream.h>
#include..string.h>
#include..conio.h>

int main()
{
char name[10][20];
char temp[20];
char ifilename[15], ofilename[15];
int num;
int i=0;
cout..."Enter how many person details you want to enter: "; cin>>num;
cout..."Enter the file name where it has to be written: "; cin>>ofilename;

ofstream ofileobj(ofilename,ios::out);
cout..."Enter person details: ";
while(i {
cin>>name[i];
ofileobj...name[i]...endl;
i++;
}
ofileobj.close();
cout..."Enter the file name where output has to be written: \n ";
cin>>ifilename;
ifstream inobj(ofilename,ios::in);
ofileobj.open(ifilename,ios::out);

cout..."The details of the person in reverse that are entered is:\n";
while(inobj >> temp != NULL)
{
cout...strrev(temp)...endl;
ofileobj...temp...endl;
}
getch();
return 0;
}