You are on page 1of 67

INFORMATION STORAGE AND RETRIEVAL LAB

1.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<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
// function to read names from standard input and display reverse of same names on standard
output //
void standard_io()
{
cout<<"\n Enter the number of names \n";
int n;
cin>>n;
cout<<"\n Enter the names \n";
char name[10][20];
int i;
char *temp1;
for(i=0;i<n;i++)
{
cin>>name[i];
temp1=(char *)malloc(sizeof(name[i]));
temp1=strrev(name[i]);
cout<<"\n"<<temp1;
}
}
// function to read the names , write them into a user defined file and then write the reverse of names
into another user defined file //
void userfile_io()
{
cout<<"\n Enter the name of the file into which names have to be stored \n";
char ifilename[10];
cin>>ifilename;
cout<<"\n Enter the name of the file into which the names will have to be written in reverse order
\n";
char revfilename[20];
cin>>revfilename;
cout<<"\n Enter the number of names \n";
int n;
cin>>n;
cout<<"\n Enter the name \n";
char name[10][10];

Department of ISE, SIT, Tumkur

-1-

INFORMATION STORAGE AND RETRIEVAL LAB

char *temp1;
ofstream out(ifilename);
int i;
for(i=0;i<n;i++)
{
cin>>name[i];
out<<name[i]<<endl;
}
out.close();
ifstream in(ifilename);
char temp[10];
out.open(revfilename,ios::out);
while(in>>temp!=NULL)
{
temp1=(char *)malloc(sizeof(temp));
temp1=strrev(temp);
out<<temp1<<endl;
}
in.close();
out.close();
}
// main function begins //
int main()
{
int ch; do
{
cout<<"\n 1-Standara input/output \n 2-User specified file input/output \n 3-Exit \n Enter
your choice \n";
cin>>ch;
if(ch==1)
standard_io();
else if(ch==2)
userfile_io();
}
while(ch<3);
return 0;
}
// main function ends //

OR

Department of ISE, SIT, Tumkur

-2-

INFORMATION STORAGE AND RETRIEVAL LAB

#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<num)
{
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;
}

2. 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

Department of ISE, SIT, Tumkur

-3-

INFORMATION STORAGE AND RETRIEVAL LAB

#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()

Department of ISE, SIT, Tumkur

-4-

INFORMATION STORAGE AND RETRIEVAL LAB

{
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");

Department of ISE, SIT, Tumkur

-5-

INFORMATION STORAGE AND RETRIEVAL LAB

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 "<<usn<<" are ";
cout<<"\nname = "<< s1[j].name;
cout<<"\nusn = "<< s1[j].usn;
cout<<"\nage = "<< s1[j].age;
cout<<"\nsem = "<< s1[j].sem;

Department of ISE, SIT, Tumkur

-6-

INFORMATION STORAGE AND RETRIEVAL LAB

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)
{

Department of ISE, SIT, Tumkur

-7-

INFORMATION STORAGE AND RETRIEVAL LAB

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

3. 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.

Department of ISE, SIT, Tumkur

-8-

INFORMATION STORAGE AND RETRIEVAL LAB

#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)

Department of ISE, SIT, Tumkur

-9-

INFORMATION STORAGE AND RETRIEVAL LAB

{
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,'|');

Department of ISE, SIT, Tumkur

- 10 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 11 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 12 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

4. 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

Department of ISE, SIT, Tumkur

- 13 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<iostream.h>
#include<fstream.h>
#define SIZEOFRECORD 66
int no_of_records;
class student
{
public:
char name[15], usn[15], branch[15], sem[15];
};
void displayontoterminal(student);
void pack(student);
void writefrombuffertofile(char[]);
void unpack(char *buffer)
{
student stu;
int i=0,j=0,count=0;
char temp[16];
while(buffer[i]!='!')
{
j=0;
while(buffer[i]!='|')
temp[j++]=buffer[i++];
i++;
count++;
temp[j]='\0';
switch(count)
{
case 1 : strcpy(stu.name,temp);
break;
case 2 : strcpy(stu.usn,temp);
break;
case 3 : strcpy(stu.branch,temp);
break;
case 4 : strcpy(stu.sem,temp);
break;
}
}
displayontoterminal(stu);
}
void writetofile()
{
student stu;
cout<<"Enter the name of the student\n";
cin>>stu.name;
cout<<"Enter the usn of the student";
cin>>stu.usn;
cout<<"Enter the branch of the student\n";
cin>>stu.branch;

Department of ISE, SIT, Tumkur

- 14 -

INFORMATION STORAGE AND RETRIEVAL LAB

cout<<"Enter the semester of the student\n";


cin>>stu.sem;
pack(stu);
}
void pack(student stu)
{
char buffer[66];
strcpy(buffer,stu.name);
strcat(buffer,"|");
strcat(buffer,stu.usn);
strcat(buffer,"|");
strcat(buffer,stu.branch);
strcat(buffer,"|");
strcat(buffer,stu.sem);
strcat(buffer,"|");
/*
cout<<"The packed data is\n";
*
cout<<buffer;*/
int count=strlen(buffer);
for(int k=0;k<65-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
writefrombuffertofile(buffer);
}
void accessrecordbyRRN()
{
int RRN;
char buffer[65];
cout<<"Enter the required Record Number\n";
cin>>RRN;
if(RRN > no_of_records)
{
cout<<"\n Invalid Record no.!!!";
return;
}
int required_record_offset = ((RRN-1)*SIZEOFRECORD);
ifstream readfromfile("puru.txt",ios::in);
readfromfile.seekg(required_record_offset,ios::beg);
readfromfile.getline(buffer,65,'\n');
cout<<"The record is\n";
cout<<buffer<<"\n";
unpack(buffer);
readfromfile.close();
}
void writefrombuffertofile(char *buffer)
{
ofstream writetofile("puru.txt",ios::app);

Department of ISE, SIT, Tumkur

- 15 -

INFORMATION STORAGE AND RETRIEVAL LAB

writetofile<<buffer;
writetofile.close();
no_of_records++;
}
void displayontoterminal(student stu)
{
cout<<"Name\t\t\tusn\t\t\tBranch\t\t\tSemester\t\t\t\n";
cout<<stu.name<<"\t\t\t"<<stu.usn<<"\t\t\t"<<stu.branch<<"\t\t\t"<<stu.sem<<endl;
}
int main()
{
int ch=1;
while(ch)
{
cout<<"\n Enter 1. write to the file \n 2. Access record by RRN \n 3. Exit\n Enterur choice:";
cin>>ch;
switch(ch)
{
case 1:
writetofile();
break;
case 2:
accessrecordbyRRN();
break;
case 3:
exit(0);
}
}
return 0;
}

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

Department of ISE, SIT, Tumkur

- 16 -

INFORMATION STORAGE AND RETRIEVAL LAB

include<iostream>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
//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,'|');

Department of ISE, SIT, Tumkur

- 17 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 18 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

Department of ISE, SIT, Tumkur

- 19 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

OR

Department of ISE, SIT, Tumkur

- 20 -

INFORMATION STORAGE AND RETRIEVAL LAB

#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,'|');

Department of ISE, SIT, Tumkur

- 21 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

Department of ISE, SIT, Tumkur

- 22 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 23 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

6. 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.

Department of ISE, SIT, Tumkur

- 24 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<iostream>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
//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++)

Department of ISE, SIT, Tumkur

- 25 -

INFORMATION STORAGE AND RETRIEVAL LAB

{
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;

Department of ISE, SIT, Tumkur

- 26 -

INFORMATION STORAGE AND RETRIEVAL LAB

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++)

Department of ISE, SIT, Tumkur

- 27 -

INFORMATION STORAGE AND RETRIEVAL LAB

{
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++)
{

Department of ISE, SIT, Tumkur

- 28 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 29 -

INFORMATION STORAGE AND RETRIEVAL LAB

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();

Department of ISE, SIT, Tumkur

- 30 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

Department of ISE, SIT, Tumkur

- 31 -

INFORMATION STORAGE AND RETRIEVAL LAB

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,'|');

Department of ISE, SIT, Tumkur

- 32 -

INFORMATION STORAGE AND RETRIEVAL LAB

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);

Department of ISE, SIT, Tumkur

- 33 -

INFORMATION STORAGE AND RETRIEVAL LAB

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++)
{

Department of ISE, SIT, Tumkur

- 34 -

INFORMATION STORAGE AND RETRIEVAL LAB

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);
}

Department of ISE, SIT, Tumkur

- 35 -

INFORMATION STORAGE AND RETRIEVAL LAB

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;

Department of ISE, SIT, Tumkur

- 36 -

INFORMATION STORAGE AND RETRIEVAL LAB

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

7. Write a C++ program to read two lists of names and then match the names in the two lists using
Cosequential Match based on a single loop. Output the names common to both the lists

Department of ISE, SIT, Tumkur

- 37 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
void writeLists()
{
fstream out1,out2;
int i,m,n;
char name[20];
out1.open("file1.txt",ios::out);
out2.open("file2.txt",ios::out);
if( (!out1) || (!out2) )
{
cout<<"Unable to open one of the list files";
getch();
exit(0);
}
cout<<"Enter no. of names you want to enter in file1"; cin>>m;
cout<<"\nEnter the names in ascending order\n";
for(i-0;i<m;i++)
{
cin>>name;
out1<<name;
out1<<'\n';
}
cout<<"Enter no. of names you want to enter in file2"; cin>>n;
cout<<"\nEnter the names in ascending order \n";
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<'\n';
}
out1.close();
out2.close();
}
void main()
{
char list1[100][20], list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;
writeLists();

Department of ISE, SIT, Tumkur

- 38 -

INFORMATION STORAGE AND RETRIEVAL LAB

out1.open("file1.txt",ios::in);
out2.open("file2.txt",ios::in);
out3.open("file3.txt",ios::out);
if( (!out3) || (!out1) || (!out2) )
{
printf("Unable to open one of the file");
getch();
exit(0);
}
clrscr();
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,'\n');
cout<<list1[m];
m++;
}
while(!out2.eof())
{
out2.getline(list2[n],20,'\n');
cout<<list2[n];
n++;
}
m--;
n--;
i=0;
j=0;
cout<<"\nElements common to both files are: \n ";
while(i<m && j<n)
{
if( strcmp(list1[i],list2[j]) == 0 )
{
out3<<list1[i];
cout<<list1[i]<<"\n";
out3<<'\n';
i++;
j++;
}
else if( strcmp(list1[i],list2[j]) < 0 )
i++;
else
j++;
}
getch();
}
8. Write a C++ program to read k Lists of names and merge them using k-way merge algorithm with
k = 8.
#include<iostream.h>

Department of ISE, SIT, Tumkur

- 39 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<fstream.h>
#include<string.h>
// Record specification
class record
{
public: char name[20];
char usn[20];
}rec[20];
int no;
fstream file[8];
//The first 8 files
char fname[8][8] = {"l.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
void merge_file(char* file1, char* file2, char* filename)
{
record recrd[20];
int k; k=0;
fstream f1,f2;
f1.open(file1,ios::in); //open the first file
f2.open(file2,ios::in); //open the second file
while(!f1.eof()) //Unpack and retrieve first file
{
f1.getline(recrd[k].name,20,'|');
f1.getline(recrd[k++].usn,20,'\n');
}
while(!f2.eof()) //Unpack and retrieve second file
{
f2.getline(recrd[k].name,20,'|');
f2.getline(recrd [k++].usn, 20,'\n');
}
record temp;
int t,y;
for(t=0;t<k-2;t++) //Sort the retrieved records
for(y=0;y<k-t-2;y++)
if(strcmp(recrd[y].name,recrd[y+1].name)>0)
{
temp=recrd[y];
recrd[y]=recrd[y+1];
recrd[y+1]=temp;
}
fstream temp1;
temp1.open(filename,ios::out);
//Open the file to be packed into
for(t=1;t<k-1;t++) //Pack the sorted records onto the file
temp1<<recrd[t].name<<"|"<<recrd[t].usn<<"\n";
f1.close();

Department of ISE, SIT, Tumkur

- 40 -

INFORMATION STORAGE AND RETRIEVAL LAB

f2.close();
temp1.close();
return;
}
void kwaymerge()
{
char filename[7][20]={"ll.txt","22.txt","33.txt","44.txt","lll.txt","222.txt","llll.txt"};
int i;
int k;
k=8;
for(i=0;i<8;i+=2) //Merge and sort the 8 original files onto
{ //the four files indicated {ll.txt,22.txt....}
merge_file(fname[i],fname[i+1],filename[k++]);
}
k=4;
for(i=0;i<4;i+=2) //Merge and sort the four files onto lll.txt and 222.txt
{
merge_file(filename[i],filename[i+1],filename[k++]);
}
//Merge and sort the two files onto the llll.txt file
merge_file(filename[4],filename[5],filename[6]);
return;
}
int main()
{
int i;
cout<<"Enter the no. of records : ";
cin>>no;
cout<<"\nEnter the details : \n";
for(i=0;i<8;i++) //Create 8 files to store the split data
file[i].open(fname[i],ios::out);
for(i=0;i<8;i++) //Split and pack data onto the files
{
cout<<"Name :"; cin>>rec[i].name;
cout<<"USN : ";cin>>rec[i].usn;
file[i%8]<<rec[i].name<<'|'<<rec[i].usn<<"\n";
}
for(i=0;i<8;i++)
file[i].close();
kwaymerge(); //Merge
fstream result;
result.open("llll.txt",ios::in);
cout<<"\nSorted Records : \n";
char name[20],usn[20];
for(i=0;i<8;i++) //Unpack the sorted records and dispL
{
result.getline(name,20,'|');
result.getline(usn,20,'\n');
cout<<"\nName : "<<name<<"\nUSN : "<<usn<<"\n";
}
return 0;

Department of ISE, SIT, Tumkur

- 41 -

INFORMATION STORAGE AND RETRIEVAL LAB

9. Write a C++ program to implement B-Tree for a given set of integers and its operations insert ( )
and search ( ). Display the tree.
// Program which maintains a B-tree of order 5.

Department of ISE, SIT, Tumkur

- 42 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
const int MAX = 4 ;
const int MIN = 2 ;
struct btnode
{
int count;
int value[MAX + 1];
btnode *child[MAX + 1];
};
class btree
{
private : btnode *root ;
public : btree( ) ;
void insert(int val) ;
int setval(int val, btnode *n, int *p, btnode **c);
static btnode *search (int val, btnode *root, int *pos);
static int searchnode(int val, btnode *n, int *pos);
void iskeypresent( int val) ;
void fillnode( int val, btnode *c, btnode *n, int k );
void split( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode );
void clear( btnode *root, int k );
void copysucc( btnode *root, int i );
void merge( int k ) ;
void show( );
static void display( btnode *root );
static void deltree( btnode *root );
~btree( );
};
// Initializes data member
btree :: btree( )
{
root=NULL;
}
// searches a key and checks whether it is present or not
void btree :: iskeypresent(int val)
{
int i ;
if( searchnode ( val, root, &i ) )
cout<< "Key found "<<endl;
else
cout<< "Key Not found "<<endl;
}

Department of ISE, SIT, Tumkur

- 43 -

INFORMATION STORAGE AND RETRIEVAL LAB

// Inserts a value in the B-tree


void btree :: insert ( int val )
{
int i ;
btnode *c, *n ;
int flag ;
flag = setval( val, root, &i, &c ) ;
if( flag )
{
n = new btnode ;
n -> count = 1 ;
n -> value[1] = i ;
n -> child[0] = root ;
n -> child[1] = c ;
root = n ;
}
}
// Sets the value in the node
int btree :: setval ( int val, btnode *n, int *p, btnode **c)
{
int k ;
if ( n == NULL )
{
*p = val ;
*c = NULL ;
return 1 ;
}
else
{
if( searchnode ( val, n, &k ) )
{
cout << endl << "Key value already exists";
return 0 ;
}
if ( setval ( val, n -> child[k], p, c ) )
{
if ( n -> count < MAX )
{
fillnode ( *p, *c, n, k ) ;
return 0 ;
}
else
{
split ( *p, *c, n, k, p, c ) ;
return 1 ;
}
}
return(0);
}
}

Department of ISE, SIT, Tumkur

- 44 -

INFORMATION STORAGE AND RETRIEVAL LAB

// Searches value in the node


btnode * btree :: search ( int val, btnode *root, int *pos )
{
if ( root == NULL ) return NULL ;
else
{
if ( searchnode ( val, root, pos ) )
return root ;
else
return search ( val, root -> child[*pos], pos ) ;
}
}
// searches for the node
int btree :: searchnode ( int val, btnode *n, int *pos )
{
//this condition is used to decide the traversal
if ( val < n -> value[1] )
{
*pos = 0 ;
return 0 ;
}
else
{
*pos = n -> count ;
while ( ( val < n -> value[*pos] ) && *pos > 1 )
(*pos)-- ;
if ( val == n -> value[*pos] )
return 1 ;
else
return 0 ;
}
}
// adjusts the value of the node
void btree :: fillnode ( int val, btnode *c, btnode *n, int k )
{
int i ;
for ( i = n -> count ; i > k ; i-- )
{
n -> value[i + 1] = n -> value[i] ;
n -> child[i + 1] = n -> child[i] ;
}
n -> value[k + 1] = val ;
n -> child[k + 1] = c ;
n -> count++ ;
}
// splits the node
void btree :: split ( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode )

Department of ISE, SIT, Tumkur

- 45 -

INFORMATION STORAGE AND RETRIEVAL LAB

{
int i, mid ;
if ( k <= MIN )
mid = MIN ;
else
mid = MIN + 1 ;
*newnode = new btnode ;
for ( i = mid + 1 ; i <= MAX ; i++ )
{
( *newnode ) -> value[i - mid] = n -> value[i] ;
( *newnode ) -> child[i - mid] = n -> child[i] ;
}
( *newnode ) -> count = MAX - mid ;
n -> count = mid ;
if ( k <= MIN )
fillnode ( val, c, n, k ) ;
else
fillnode ( val, c, *newnode, k - mid ) ;
*y = n -> value[n -> count] ;
( *newnode ) -> child[0] = n -> child[n -> count] ;
n -> count-- ;
}
// removes the value from the node and adjusts the values
void btree :: clear ( btnode *root, int k )
{
int i ;
for ( i = k + 1 ; i <= root -> count ; i++ )
{
root -> value[i - 1] = root -> value[i] ;
root -> child[i - 1] = root -> child[i] ;
}
root -> count-- ;
}
// copies the successor of the value that is to be deleted
void btree :: copysucc ( btnode *root, int i )
{
btnode *temp = root -> child[i] ;
while ( temp -> child[0] )
temp = temp -> child[0] ;
root -> value[i] = temp -> value[1] ;
}
// merges two nodes
void btree :: merge ( int k )
{
btnode *temp1, *temp2 ;
temp1 = root -> child[k] ;
temp2 = root -> child[k - 1] ;
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = root -> value[k] ;

Department of ISE, SIT, Tumkur

- 46 -

INFORMATION STORAGE AND RETRIEVAL LAB

temp2 -> child [temp2 -> count] = root -> child[0] ;


for ( int i = 1 ; i <= temp1 -> count ; i++ )
{
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = temp1 -> value[i] ;
temp2 -> child[temp2 -> count] = temp1 -> child[i] ;
}
for ( i = k ; i < root -> count ; i++ )
{
root -> value[i] = root -> value[i + 1] ;
root -> child[i] = root -> child[i + 1] ;
}
root -> count-- ;
delete temp1 ;
}
// calls display( )
void btree :: show( )
{
display ( root ) ;
}
// displays the B-tree
void btree :: display ( btnode *root )
{
if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++)
{
display ( root -> child[i] ) ;
cout << root -> value[i + 1] << "\t";
}
display ( root -> child [i] ) ;
}
}
// deallocates memory
void btree :: deltree ( btnode *root )
{
if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++)
{
deltree ( root -> child [i] ) ;
delete ( root -> child[i] ) ;
}
deltree ( root -> child[i] ) ;
delete ( root -> child[i] ) ;
}
}

Department of ISE, SIT, Tumkur

- 47 -

INFORMATION STORAGE AND RETRIEVAL LAB

btree :: ~btree( )
{
deltree ( root ) ;
}
void main( )
{
btree b ;
clrscr();
int num;
int choice=0;
while (choice!=4)
{
cout<<endl<<"l - Insert"<<endl;
cout<<"2 - Search"<<endl;
cout<<"3 - Display"<<endl;
cout<<"4 - Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"\nEnter the elements to be inseretd into b tree \n";
cin>>num;
b.insert ( num ) ;
break;
case 2: cout<<"\nEnter the elements to be searched in b tree \n";
cin>>num;
b.iskeypresent ( num ) ;
break;
case 3: b.show( ) ;
break;
case 4: exit(0);
default: cout<<"\nInvalid Option \n";
}
}
}

10. Write a C++ program to implement B+ tree for a given set of integers and its operations insert ( ),
and search ( ). Display the tree.
#include<iostream.h>

Department of ISE, SIT, Tumkur

- 48 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<math.h>
#include<conio.h>
struct node
{
int ele[4];
int child[4];
node *next;
};
class bptree
{
public:
node *tree[20][20];
int count[20];
int leaf;
int path[20];
node *head;
bptree();
node* create_node();
void insert(int);
void main_search(int);
void display_tree();
void insert_node(node*,int);
void search(int);
int search_node(node*,int);
int nodefull(node*);
void split(node*);
};
bptree::bptree()
{
leaf=-1;
for(int i=0;i<10;i++)
{
count[i]=-1;
path[i]=-1;
}
}
node* bptree::create_node()
{
node *n;
n= new node;
for(int i=0;i<4;i++)
{
n->ele[i]=-1;
n->child[i]=-1;
}
n->next=NULL;
return n;
}

Department of ISE, SIT, Tumkur

- 49 -

INFORMATION STORAGE AND RETRIEVAL LAB

void bptree::insert(int key)


{
int n,parent;
node *first_node;
if(leaf==-1)
{
first_node=create_node();
tree[0][0]=first_node;
leaf++;
count[0]++;
first_node->ele[0]=key;
head=first_node;
//header node of sequence set
}
else if(leaf==0)
{
if(nodefull(tree[0][0]))
{
path[leaf]=0;
split(tree[0][0]);
insert(key);
}
else insert_node(tree[0][0],key);
}
else
{
search(key);
n=path[leaf];
parent=path[leaf-1];
if((nodefull(tree[leaf][n])))
{
split(tree[leaf][n]);
insert(key);
}
else
insert_node(tree[leaf][n],key);
}
}
void bptree::main_search(int key)
{
int flag=0,i;
node *node1;
search(key);
node1=tree[leaf][path[leaf]];
for(i=0;node1->ele[i]!=-1;i++)
if(node1->ele[i]==key)
{

Department of ISE, SIT, Tumkur

- 50 -

INFORMATION STORAGE AND RETRIEVAL LAB

flag=1;
break;
}
cout<<"\n The Path traversed is:";
for(i=0;path[i]!=-1;i++)
cout<<path[i]<<"->";
if(flag)
cout<<"\nElement Found";
else cout<<"\nNot Found";
}
void bptree::display_tree()
{
int i,j,k;
for(i=0;i<=leaf;i++)
{
cout<<"\n\n Level-------------"<<i<<"\n";
for(j=0;j<=count[i];j++)
{
if(i!=leaf)
k=1;
else k=0;
//print first element only at leaf level
for(;tree[i][j]->ele[k] != -1;k++)
cout<<""<<tree[i][j]->ele[k];
cout<<"\t";
}
}
}
void bptree::search(int key)
{
int i,j,temp;
path[0]=0; //always start the path from root
if(leaf)
{
j=0;
for(i=0;i<leaf;i++)
{
temp=search_node(tree[i][j],key);
path[i+1]=temp;
j=temp;
}
}
}
int bptree::search_node(node *node1,int key)
{
for(int i=0;i<4;i++)
{
if(key < node1->ele[i])

Department of ISE, SIT, Tumkur

- 51 -

INFORMATION STORAGE AND RETRIEVAL LAB

return node1->child[i];
if((key >= node1->ele[i]) && (key < node1->ele[i+1]))
return node1->child[i];
else if(node1->ele[i+1] == -1)
return node1->child[i];
}
}
int bptree::nodefull(node *node1)
{
if(node1->ele[3]!=-1)return 1;
else return 0;
}
void bptree::insert_node(node *node1,int key)
{
int flag=0,count=-1,i,j,x,y,l;
node *newnode, *parent;
for(i=0;i<4;i++) if(node1->ele[i]!=-1) ++count;
i=0;
while(!flag && node1->ele[i] != -1)
{
if(node1->ele[i] > key)
{
flag=1;
for(int j=count; j>=i; j--)
node1->ele[j+1]=node1->ele[j];
node1->ele[i]=key;
}
i++;
}
if(!flag)
node1->ele[count+1]=key;
if(node1->ele[0]==key)
{
for(i=leaf-1;i>=0;i--)
{
x=path[i+1];
if(tree[i][path[i]]->ele[x]>key)
tree[i][path[i]]->ele[x]=key;
else
insert_node(tree[i][x],key);
}
}
for(i=0;i<=count+1;i++)
cout<<"\t\t"<<node1->ele[i];
}
void bptree::split(node *oldnode)
{

Department of ISE, SIT, Tumkur

- 52 -

INFORMATION STORAGE AND RETRIEVAL LAB

node *newnode, *parent,*n1,*n2;


node *temp;
int i,j,k,n,t,x,y,pos;
newnode=create_node();
newnode->ele[0]=oldnode->ele[2];
newnode->ele[1]=oldnode->ele[3];
oldnode->ele[2]=-1;
oldnode->ele[3]=-1;
t=count[leaf];
n=path[leaf];
for(i=t,j=t+1;i>n;i--,j--)
tree[leaf][j]=tree[leaf][i];
newnode->next=tree[leaf][n]->next;
tree[leaf][n]->next=newnode;
tree[leaf][n+1]=newnode;
count[leaf]++;
x=leaf;
if(count[leaf]+1==1)
t=1;
else
t=log(count[leaf]+1)/log(2);
if(t!=leaf)
{
++leaf;
count[leaf]=count[x];
for(i=0;i<=count[leaf];i++)
{
temp=tree[leaf][i];
tree[leaf][i]= tree[x][i];
tree[x][i]= temp;
}
}
for(i=leaf-1;i>=0;i--)
count[i]=-1;
for(i=t,j=i-1;i>0;i--,j--)
{
for(k=0;k<=count[i]/3;k++)
{
n1=tree[i][2*k];
n2=tree[i][(2*k)+1];
newnode=create_node();
count[j]++;
tree[j][count[j]]=newnode;
newnode->ele[0]=n1->ele[0];
newnode->child[0]=2*k;
newnode->ele[1]=n2->ele[0];
newnode->child[1]=(2*k)+1;
}
if(count[i] != 1 && count[i]%2 == 0)
{

Department of ISE, SIT, Tumkur

- 53 -

INFORMATION STORAGE AND RETRIEVAL LAB

n2=tree[i][count[i]];
newnode->ele[2]=n2->ele[0];
newnode->child[2]=count[i];
}
}
}
int main()
{
bptree bt;
int choice,key;
clrscr();
while(1)
{
cout<<"\n\nMain Menu\n-----------------------\n 1.Insert\n 2.Search\n 3.DisplayTree\n 4.Exit\n\n Enter your
choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Enter the element:";
cin>>key;
bt.insert(key);
break;
case 2:cout<<"Enter the key:";
cin>>key;
bt.main_search(key);
break;
case 3:bt.display_tree();
break;
case 4:return 0;
default:cout<<"\nEnter valid choice";
}
}
}

11. Write a C++ program to store and retrieve student data from file using hashing. Use any collision
resolution technique
#include<stdio.h>

Department of ISE, SIT, Tumkur

- 54 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
//Record specification
class node
{
public: char name[15],usn[15];
node *link;
};
node *h[29] //Array of record pointers equal to size of hash keys - 29
void insert()
{
char name[15], usn[15], buffer[50];
istream in;
ofstream out;
out.open("student.txt",ios::app); //opening student.txt in append mode
if(!out)
{
cout<<"\nUnable to open the file in append mode";
getch();
return;
}
cout<<"\nEnter the name = "; cin>>name;
cout<<"\n Enter the usn = "; cin>>usn;
strcpy(buffer,name); //Packing the record onto the file using '|' as a delimiter
strcat(buffer,"|");
strcat(buffer,usn);
strcat(buffer,"\n"); // \n delimiter for the record
out<<buffer;
// appending the packed record onto the file
out.close();
}
//Insert record into the hash table
void hash_insert(char name1[], char usn1[], int hash_key)
{
node *p,*prev,*curr;
p = new node; //dynamically allocate space using 'new'
strcpy(p->name,name1);
strcpy(p->usn,usn1);
p->link=NULL;
prev=NULL;
curr=h[hash_key];
if(curr==NULL)
//getting the hash pointer location Case: No collision
{

Department of ISE, SIT, Tumkur

- 55 -

INFORMATION STORAGE AND RETRIEVAL LAB

h[hash_key]=p;
return;
}
while(curr!=NULL)
{
prev=curr;
curr=curr->link;
}
prev->link=p;

// Case : On collision - Insert at rear end

}
void retrive()
{
fstream in;
char name[15],usn[15];
int j,count;
node *curr;
in.open("student.txt",ios::in); // open the record file in input mode
if(!in)
{
cout<<"\n Unable to open the file in input mode";
getch();
exit(0);
}
while(!in.eof())
{
in.getline(name,15,'|');
//unpacking the record
in.getline(usn,15,'\n');
count=0;
for(j=0; j<strlen(usn); j++) //Calculate sum of ascii values of USN
{
count=count+usn[j];
}
count=count%29;
//Hash Key = ASCII count% 29
hash_insert(name,usn,count);
}
cout<<"\n Enter the usn = "; cin>>usn;
count=0;
for(j=0; j<strlen(usn); j++)
// Calculating Hash Key
count=count+usn[j];
count=count%29;
curr=h[count];
if(curr == NULL)
{
cout<<"\nRecord not found";
getch();
return;
}
do
{
if(strcmp(curr->usn,usn)==0)
//When the record is found, retrieve
{

Department of ISE, SIT, Tumkur

- 56 -

INFORMATION STORAGE AND RETRIEVAL LAB

cout<<"\nRecord found : "<<curr->usn<<" "<<curr->name;


getch();
return;
}
else
{
curr=curr->link;
}
}while(curr!=NULL);

//Search till end of list

if(curr==NULL) //End of list reached with no record found


{
cout<<"\nRecord not found";
getch();
return;
}
}
void main()
{
int choice;
clrscr();
fstream out;
out.open("student.txt",ios::out);
if(!out)
{
cout<<"\nUnable to open the file in out mode";
getch();
exit(0);
}
for(;;)
{
cout<<"\n1:insert\n 2: retrive \n3:exit \nEnter the choice - ";
cin>>choice;
switch(choice)
{
case 1: insert();
break;
case 2: retrive();
break;
case 3: exit(0);
default: cout<<"\nInvalid option";
}
}
}

12. Write a C++ program to reclaim the free space resulting from the deletion of records using linked
lists.
#include<stdio.h>

Department of ISE, SIT, Tumkur

- 57 -

INFORMATION STORAGE AND RETRIEVAL LAB

#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<new.h>
class node
{
public: char name[20];
char usn[20];
node *link;
};
node *first=NULL;
void writeFile()
{
node *p;
char buffer[100];
fstream out;
out.open("student.txt", ios::out);
if(!out)
{
cout<<"\n Unable to open the file student.txt in out mode";
getch();
exit(0);
}
p=first;
while(p!=NULL)
{
strcpy(buffer,p->name);
strcat(buffer,"|");
strcat(buffer,p->usn);
strcat(buffer,"\n");
out<<buffer;
p=p->link;
}
}
void display()
{
node *p;
if(first==NULL)
{
cout<<"\nList is empty";
return;
}
p=first;
while(p!=NULL)
{
cout<<"|"<<p->name<<" "<<p->usn<<"|"<<"->";
p=p->link;

Department of ISE, SIT, Tumkur

- 58 -

INFORMATION STORAGE AND RETRIEVAL LAB

}
}
void Insert()
//Insert the record at the rear end
{
char name[20],usn[15];
node *p,*q;
cout<<"\n Enter name = "; cin>>name;
cout<<"\nEnter usn = "; cin>>usn;
p=new node;
strcpy(p->name,name);
strcpy(p->usn,usn);
p->link=NULL;
if(first==NULL)
{
first=p;
writeFile();
display(); //display the record on the screen
return;
}
for(q=first; q->link!=NULL; q=q->link)
{
;
}
q->link=p;
writeFile();
//writing the record to the file
display();
//display the records to the screen.
}
void Delete()
{
char usn[15];
node *curr,*prev,*del;
if(first==NULL)
{
printf("\nThe list is empty. Deletion is not possible");
return;
}
cout<<"\nEnter the usn to be deleted = "; cin>>usn;
if(strcmp(first->usn,usn)==0)
{
cout<<"\n Record deleted";
del = first;
delete del;
first=first->link;
writeFile();
return;
}
prev=NULL;
curr=first;
while( ( strcmp(curr->usn,usn) != 0 ) && curr!=NULL)
{

Department of ISE, SIT, Tumkur

- 59 -

INFORMATION STORAGE AND RETRIEVAL LAB

prev=curr;
curr=curr->link;
}
if(curr == NULL)
{
cout<<"\nThe student with usn "<<usn<<" is not present";
return;
}
prev->link = curr->link;
writeFile();
display();
//display the records to the screen
}
void main()
{
int ch;
clrscr();
for(;;)
{
cout<<"\n 1-Insert_rear \n 2-Delete_id \n 3-Exit \n Enter choice";
cin>>ch;
switch(ch)
{
case 1: Insert();
break;
case 2: Delete();
break;
case 3: exit(0);
default: cout<<"\n Invalid option";
break;
}
}
}

Appendix-I
A file structure is a combination of representations for the data in files and of operations for accessing the
data.

Department of ISE, SIT, Tumkur

- 60 -

INFORMATION STORAGE AND RETRIEVAL LAB

Operations on file handling in C++


1.Class stream -In C++ ,we open a file by linking it to a stream. For obtaining a stream we have three
types
i) input
- ifstream in:
ii) output
- ostream out:
iii) input and output
- fstream io:
Once you create a stream, one way to associate it with a file is by using open( )
2.Opening a file The member function open ( ) is used to open a file
open (filename, mode):
filename- is a sting that holds the name of the file on disk
Mode- is optional parameter with a combination of the following flags.
ios::in
-open for reading only
ios::out
-open for writing only
ios::app -append to end of file
ios::ate
-set initial position at the end of a file
ios::binary - binary file
ios::trunc -delete contents of file if it exists.
Prototype for stream classes is shown below
void ifstream::open(const char *filename. ios::openmode mode=ios::in);
void ofstream::open(const char *filename. ios::openmode mode=ios::out ios::trunc);
void fstream::open(const char *filename. ios::openmode mode=ios::in ios::out);
This can be done using stream member function close ( )
4. Reading a file- there are several ways of reading a text from a file.but all of them have a common
approach as follows
open the file
read the file
close the file
5.Writing a file- writing to a text file can be accomplished with stream operators and it follows
open a file
write a file
close a file
6. Seeking a position in a fileifstream, has a pointer called the get pointer that points to the next element to be read.
ofstream, has a pointer called the put pointer that points to the location where the next element has to be
written.
The stream classes support the following functions to manage the pointers:
seekg( )-moves get pointer to specific location
seekg(offset,position)
seekp( )- moves put pointer to a specific location
seekp(offset,position)
tellg( )-gives the current position of get pointer
tellp( )-gives the current position of put pointer

Department of ISE, SIT, Tumkur

- 61 -

INFORMATION STORAGE AND RETRIEVAL LAB

Where offset indicates the number of bytes the file pointer is moved from the location specified by position.
Position will take the following constraints defined in the ios class
ios::beg offset count from the beginning of the stream
ios::cur offset counted from the current position of the stream
ios::end - offset counted from the end of the stream.

Appendix-II
I/O redirection and pipes in UNIX
Usually, the output is displayed on the terminal but in some cases if the input of a file listing the program is
needed as i/p to another program or if the o/p is to be stored in a file than displaying on terminal or if i/p to
a terminal or if i/p to a program is taken from a file, than a common method called I/O redirection and pipes
are used.
Here Operating system switches from standard I/O and regular file I/O. there are three streams in UNIX
which are given below
Standard input-the file representing input, usually connected to keyboard
< file (redirects standard input to file)
Standard output-the file representing put, usually connected to
> file (redirects standard output to file)
Standard error- the file representing error messages that emanate from command or shell, usually
connected to display.
UNIX provides tools for redirecting the output of one program as input to other program. the mete character
is used for this purpose.

Appendix-III

Department of ISE, SIT, Tumkur

- 62 -

INFORMATION STORAGE AND RETRIEVAL LAB

Program in C++ to display the contents of a file


Lots do some reading and writing to see how these functions are used. The first simple file
processing program opens a file for input and reads it, char by char, sending each character to the screen
after it is read from the file. This program includes the following steps:1. Display a prompt for the name of the input.
2. Read the users response from the keyboard into a variable called filename.
3. Open the file for input.
4. While these are still characters to be read from the input file,
i) Read a char from the file.
ii) Write the char to the terminal screen.
5. Close the input file.
In C++ stream:#include<iostream.h>
main()
{
Char ch;
Fstream file;
Char filename[20];
cout<<Enter the name of the file:<<flush;
cin>>filename;
file.open(filename,ios::in);
while
{
file>>ch;
if(file.fail()) break;
cout<<ch;
}
file.close();
}

Appendix-IV
Unix file system commands
Unix provides many commands for manipulating files.
Cat filenames:- Print the contents of the named text files.
Tail filename:- Prints the last ten lines of the text file.
Cp file1 file2:- Copy file1 to file2.
Mv file1 file2:- Move(rename) file1 to file2.
Rm filenames:- Remove(delete) the named files.
Chmod mode filename:- Changes the protection mode on the named file.
Ls:- List the contents of the directory.
Mkdir name:- Create a directory with the given name.
Rmdis name:- Remove the named directory

Appendix-V

Department of ISE, SIT, Tumkur

- 63 -

INFORMATION STORAGE AND RETRIEVAL LAB

Field Structures
A filed is the smallest logical meaningful unit of information in a file
There are many ways of adding structures to files to maintain the identity of the fields. Four of the most
common methods follow:1.
2.
3.
4.
5.

Force the fields into a predictable length.


Begin each field with a length indicator.
Place a delimiter at the end of each fields to separate it from the next fields.
Use a Keyword=Value expression to identify each fields and its contents.

Fig (a)
Ames
Mary
123 Maple
Stillwater
OK74075
Mason
Alan
90 Eastgate
Ada
OK74820
<----10------><---10-----><--------10-----------><----------10---------><-------10------>
fig (b)
04Ames04Mary09123 Maple10Stillwater020OK74075
05Mason04Alan1190 Eastgate03Ada02OK74820
fig (c)
Ames | Mary| 123 Maple | Stillwater
| OK | 74075
Mason | Alan | 90 Eastgate | Ada | OK | 74820
fig (d)
last=Ames | first=Mary | address=123 Maple | city=Stillwater | state=OK | zip=74075
last=Mason | first=Alan | address=90 eastgate | city=Ada | state=OK | zip=74820
Reading a Stream of Fields
istream & opreator >> (istream & stream, person &p)
{ //read delemeted fields from file
char delim;
stream.getline(p.lastname,30,'1');
if (strlen(p.lastname==0)) return stream;
stream.getline(p.lastname,30,'1');
stream.getline(p.address,30,'1');
stream.getline(p.city,30,'1');
stream.getline(p.state,15,'1');
stream.getline(p.zipcode,10,'1');
}
The output of above program looks like this for delimited-field version
LastName
'Ames'
FisrtName
'Mary'
Address
'123 Maple'

Appendix-VI

Department of ISE, SIT, Tumkur

- 64 -

INFORMATION STORAGE AND RETRIEVAL LAB

Record Structure
A Record can be defined as a set of fields that belong together when the file is viewed in terms of a
higher level of organization. Like the notion of a filed, a record is another conceptual tool. Records do not
necessarily exist in the logical notion included in the file's structure.
A record in a file represents a structured data objects. The C++ programming examples are focused
on adding methods to classes to support using files to preserve the state of objects.
Following are some of the most often used methods for organizing the records of a file:
Method 1:- Make Records a Predictable Number of Bytes(Fixed-Length Records)
A fixed-length record file is one in which each record contains the same number of bytes. Fixed
length records are frequently used as containers to hold variable number of variable fields. It is also possible
to mix fixed & variable length field within a record.
Fig (a) Fixed-Length records with fixed-length fields
Ames
Mason

Mary
Alan

123 Maple
90 Eastgate

Stillwater
Ada

OK74075
OK74820

Fig (b) fixed length records with variable length fields


Ames: Mary:123 Maple:Stillwater:OK74075:<----------------unused space------------------------>
Mason :Alan:90 Eastgate:Ada:OK74820:<----------------------unused space----------------------->
Method 2:- Make Records a Predictable Number of Fields
Rather than specify that each record in a file contain some fixed number of bytes. We can specify
that it will contain a fixed number of fields. This is a good way to organize the records looking at
fig (c) six fields per record.
1
2
3
4
5
6
Ames : Mary : 123 Maple : Stillwater : OK : 74075 :Mason : Alan : 90 : Eastgate : Ada : OK : 74820
Methods 3:- Begin each Record With a Length Indicator
We can communicate the length of records by beginning each record with a field containing an
integer that indicates how many bytes there are in the rest of the record
This is commonly used method for handling variable length records.
04Ames : Mary : 123 Maple : Stillwater : OK : 74075 :36 Mason : Alan : 90 Eastgate : Ada : OK | 74820
Method 4:-Use an Index to keep track of address
We can use an index to keep a byte offset for each record in the originate file. The byte offsets
allow us to find the beginning of each successive record and compute the length of each record. We look up
the portion of a record in the index then seek to the record in the data file.
Fig (e) illustrate this two file mechanism

fig (e)

Department of ISE, SIT, Tumkur

- 65 -

INFORMATION STORAGE AND RETRIEVAL LAB

Data Files:
Ames:Mary:123:Maple:stillwater:OK:74075:Mason:Alan:90: Eastgate:Ada:OK74820
Index file:
00

40

Method 5:-Place a Delimiter at the end of each Record


fig (f)
Ames:Mary:123Maple:Stillwater:OK:74075:#Mason:Alan:90 EastGate:Ada:OK74820
A Record Structure That uses a length indicator.
Selection of a method for record organization depends on the nature of the data and on what you
need to do with it. We begin by looking at a record structure that uses a record-length at the beginning of the
record. This approach lets us preserve the variabilitys in the length of records that is inherit in our stream
file.
Writing the variable-length records to the file
Implementing variable length records is practically a matter of building on the program in
writstream.cpp, but it also involves addressing some new problems.
If we want to put a length indicator at the beginning of every record(before any other field) we must
know the sum of lengths of the fields in each record before we can begin. Writing a record to the file, we
need to accumulate the entire content of the record in a buffer before writing it out.
In what form should we write the record length field to the file? As a binary integer?
The concepts of buffering are one we run into again and again as we work with files. In this case,
the buffer can simply be a character array into which we place the fields and field Delimiter as we collect
them. A C++ function writeperson, written using the C string function, is found in the below program. This
function creates a buffer; fills it with the delimited field values using strcat, the string concatenation
function; calculates the length of the buffer using strlen; then writes the buffer length to the buffer to the
output stream.
Const int MaxBuffersize=200;
int WritePerson (ostream & stream, person & p)
{
char buffer [MaxBufferSIze]; //creates a buffer of fixed size
Strcpy(buffer,p.lastName); Strcat(buffer,|);
Strcat(buffer,p.FirstName); Strcat(buffer,|);
Strcat(buffer,p.Address); Strcat(buffer,|);
Strcat(buffer,p.city); Strcat(buffer,|);
Strcat(buffer,p.state); Strcat(buffer,|);
Strcat(buffer,p.zipcode); Strcat(buffer,|);
short length=strlen(buffer);
stream.write(&length, size of (length)); //write length
stream.write(&buffer, length);
}
Reading the variable -length records from the file

Department of ISE, SIT, Tumkur

- 66 -

INFORMATION STORAGE AND RETRIEVAL LAB

Given our file structure of variable -length records proceeded by record length fields, it is easy to
write a program that reads through file, record by record displaying the fields from each of the records on
the screen .The program must read the length of a record, move the characters of the record into a buffer,
then break the record into fields .The code to read and break up the record is included into function read
variable person in the following program fragment,
In the read variable person (istream and stream, person and p)
{
//read a variable sized record from stream and store it in p//
short length;
stream read(&length,size of(length));
char *buffer=new char[length +1];
strem.read(buffer,length);
buffer[length]=0;
istrem strbuff(buffer);
strbuff>>p;
return 1;
}

Department of ISE, SIT, Tumkur

- 67 -

You might also like