File Structure Lab Manual
File Structure Lab Manual
LAB MANUAL
Prepared By Approved by
Programs List:
PART A
1. Write a 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.
2. Write a program to read and write student objects with fixed-length records and the fields
delimited by <|=. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
3. Write a program to read and write student objects with Variable - Length records using any
suitable record structure. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
4. Write a 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.
5. Write a program to implement simple index on primary key for a file of student objects.
Implement add ( ), search ( ), delete ( ) using the index.
6. Write a program to implement index on secondary key, the name, for a file of student
objects. Implement add ( ), search ( ), delete ( ) using the secondary index.
7. Write a program to read two lists of names and then match the names in the two lists using
Consequential Match based on a single loop. Output the names common to both the lists.
8. Write a program to read k Lists of names and merge them using k-way merge algorithm with
k = 8.
PART B MINI PROJECT
Student should develop mini project on the topics mentioned below or similar applications Document
processing, transaction management, indexing and hashing, buffer management, configuration
management. Not limited to these.
Laboratory Outcomes: The student should be able to:
Implement operations related to files
Apply the concepts of file system to produce the given application.
Evaluate performance of various file systems on given parameters.
Conduct of Practical Examination:
All laboratory experiments, excluding the first, are to be included for practical examination.
Experiment distribution
o For questions having only one part: Students are allowed to pick one experiment from the
lot and are given equal opportunity.
o For questions having part A and B: Students are allowed to pick one experiment from
part A and one experiment from part B and are given equal opportunity.
Operating systems provide shortcuts for switching between standard I/O(stdin and stdout)
and
regular file I/O. I/O redirection is used to change a program so it writes its output to a
regular file rather than to stdout.
In both DOS and UNIX, the standard output of a program can be redirected to a file with
the > symbol.
In both DOS and UNIX, the standard input of a program can be redirected to a file with
the < symbol.
The notations for input and output redirection on the command line in Unix are
Example:
The output of the executable file is redirected to a file called <myfile=
pipe
File I/O:
Standerd I/O:
Standard streams are pre connected input and output channels between a computer
program and its environment (typically a text terminal) when it begins execution. The
three I/O connections are called standard input (stdin), standard output (stdout) and
standard error (stderr).
Fstream
fstream provides an interface to read and write data from files as input/output streams.
The file to be associated with the stream can be specified either as a parameter in the
constructor or by calling member open.
Page 1
After all necessary operations on a file have been performed, it can be closed (or
disassociated) by calling member close. Once closed, the same file stream object may be
used to open another file.
The first operation generally performed on an object of one of these classes is to associate it to a
real file. This procedure is known as to open a file.
An open file is represented within a program by a stream object (an instantiation of one of these
classes, in the previous example this was myfile) and any input or output operation performed on
this stream object will be applied to the physical file associated to it.
In order to open a file with a stream object we use its member function open():
Where filename is a null-terminated character sequence of type const char * (the same type that
string literals have) representing the name of the file to be opened, and mode is an optional
parameter with a combination of the following flags:
Page 2
4. ios::ate : Set the initial position at the end of the file.If this flag is not set to
any value, the initial position is the beginning of the file.
5. ios::app : All output operations are performed at the end of the file,
appending the content to the current content of the file. This flag
can only be used in streams open for output-only operations.
6. ios::trunk : If the file opened for output operations already existed before, its
previous content is deleted and replaced by the new one.
Prototypes:
int close (int Handle);
Example:
close (Input);
Getline Function:
Extracts characters from specified location and stores them into str until the delimitation
character delimis found or length equal to size.
prototype
fstream str;
//C Program to read a data from the file and display on the monitor
#include<stdio.h>
#include<conio.h>
void main()
{ /* Output
FILE *fp; Basavaraju S
char c; Asst. Prof, Dept. Of ISE
clrscr(); Brindavan College of Engineering
fp = fopen("one.txt","r"); */
while(fread(&c,1,1,fp)!=0)
fwrite(&c,1,1,stdout);
fclose(fp);
getch();
}
Page 3
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char ch;
fstream file;
char fname[20];
clrscr();
cout<<"Enter the file name \n";
cin>>fname;
file.open(fname,ios::in);
file.unsetf(ios::skipws);
while(1)
{
file>>ch;
if(file.fail()) break;
cout<<ch;
}
file.close();
getch();
}
/* Output
Enter the file name
one.txt
Basavaraju S
Asst. Prof, Dept. Of ISE
Brindavan College of Engineering
*/
Page 4
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.
//Implementation
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include <process.h>
main()
{
int ch;
fstream fp,fp1;
char name[20][20],infilename[20],str[10],ofilename[20];
clrscr();
for(;;)
{
cout<<"\n1:Standard Input to Standard Output \n2:File to Standard Output
\n3:File to File\n4:Quit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter 5 names\n";
for(int i=1;i<=5;i++)
{
cin>>name[i];
}
for(i=1;i<=5;i++)
{
strrev(name[i]);
cout<<endl<<name[i];
}
break;
case 2:
cout<<"Enter input filename \n";
cin>>infilename;
fp.open(infilename,ios::in);
if (fp.fail())
{
cout << "Cannot open file\n";
Page 5
while(1)
{
fp>>str;
cout<<strrev(str)<<"\n";
if(fp.eof()) break;
}
fp.close();
break;
case 3:
cout<<"Enter input filename\n";
cin>>infilename;
cout<<"Enter output filename\n";
cin>>ofilename;
fp.open(infilename,ios::in);
fp1.open(ofilename,ios::out);
if (fp.fail() || fp1.fail())
{ cout << "Cannot open file\n";
getch(); exit(0);
}
while(1)
{
fp>>str;
cout<<"\n"<<str<<"\t";
fp1<<strrev(str)<<"\n";
if(fp.eof()) break;
}
fp.close();
fp1.close();
break;
default:exit(0);
}
}
}
/* Output
Run 1
Enter Your Choice
Page 6
1
Enter 5 names
Arun
Bavana
Chethan
Divya
Eshwar
nurA
anavaB
nahtehC
ayviD
rawhsE
Run 2
Enter Your Choice 2
2
Enter input filename
student.txt
201|mar
ram|102
Page 7
2. Write a C++ program to read and write and student objects with fixed-length records
and the fields delimited by <|=.implement pack(),unpack(),modify() and search() methods.
Fixed length record
A record which is predetermined to be the same length as the other records in the file
The file is divided into records of equal size.
All records within a file have the same size.
Different files can have different length records.
Programs which access the file must know the record length.
Offset, or position, of the nth record of a file can be calculated.
There is no external overhead for record separation.
There may be internal fragmentation (unused space within records.)
There will be no external fragmentation (unused space outside of records) except for
deleted
records.
Individual records can always be updated in place
//Implementation
#include<iostream.h>
#include<string.h>
#include<fstream.h>
//#include<ostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#define LEN 48
class student
{
char name[25],usn[11],age[3],buf[LEN];
void pack();
void unpack();
public:
void add();
void display();
void search();
void modify();
};
void student::pack()
{
strcpy(buf,usn);
strcat(buf,"|");
strcat(buf,name);
strcat(buf,"|");
strcat(buf,age);
for(int i=strlen(buf); i<LEN; i++) // LEN is the length of the record fixed
Page 8
strcat(buf,"#");
}
void student::unpack()
{
strcpy(usn,strtok(buf,"|"));
strcpy(name,strtok(NULL,"|"));
strcpy(age,strtok(NULL,"#"));
}
void student::add( )
{
cout<<"Enter USN: ";
cin>>usn;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter age: ";
cin>>age;
pack( );
ofstream fout;
fout.open("student.txt",ios::out|ios::app);
fout<<buf<<endl;
fout.close( );
cout<<"Student record added\n";
}
void student::display( )
{
ifstream fin;
fin.open("student.txt");
while(!fin.eof( ))
{
fin>>buf;
if(fin.fail( ))
break;
Page 9
void student::search( )
{
ifstream fin;
fin.open("student.txt");
char usn_key[11];
cout<<"Enter the USN: ";
cin>>usn_key;
while(!fin.eof( ))
{
fin>>buf;
if(fin.fail( ))
break;
unpack( );
if(!strcmp(usn,usn_key))
{
cout<<"\nStudent Found!"<<"\nUSN: "<<usn<<"\nName:
"<<name<<"\nAge:"<<age<<endl;
fin.close( );
return;
}
}
cout<<"\nStudent not found\n";
fin.close( );
}
void student::modify( )
{
fstream f;
f.open("student.txt",ios::in|ios::out);
char usn_key[11];
cout<<"Enter the key: ";
cin>>usn_key;
while(!f.eof( ))
{
f>>buf;
if(f.fail( ))
break;
unpack( );
if(!strcmp(usn,usn_key))
{
cout<<"\nStudent found\n"<<"Enter new name: ";
cin>>name;
cout<<"Enter new age: ";
cin>>age;
pack( );
Page 10
/* int pos=f.tellg()-LEN;
cout<<"pos"<<pos;*/
f.seekp((int)f.tellg( )-LEN,ios::beg);
f<<buf;
f.close( );
return;
}
}
cout<<"\nStudent not found\n";
f.close( );
}
void main()
{
student s;
int ch;
clrscr();
do
{
cout<<"\n1. Add\n2. Display\n3. Search\n4. Modify\n5.Exit\n\nEnter choice: ";
cin>>ch;
switch(ch)
{
case 1:
s.add();
break;
case 2:
s.display();
break;
case 3:
s.search();
break;
case 4:
s.modify();
break;
case 5:
exit(0);
break;
default:
cout<<"Wrong choice!";
}
}while(ch!=5);
}
Page 11
3. Write a C++ program to read and write and student objects with variable-length records
using any suitable record structure. Implement pack(),unpack(),modify() and search()
methods
Delimited record
A variable length record which is terminated by a special character or sequence of characters
Delimiter
A special character or group of characters stored after a field or record, which indicates the end
of the preceding unit.
The records within a file are followed by a delimiting byte or series of bytes.
The delimiter cannot occur within the records.
Records within a file can have different sizes.
Different files can have different length records.
Programs which access the file must know the delimiter.
Offset, or position, of the nth record of a file cannot be calculated.
There is external overhead for record separation equal to the size of the delimiter per
record.
There should be no internal fragmentation (unused space within records.)
There may be no external fragmentation (unused space outside of records) after file
updating.
Individual records cannot always be updated in place
//Implementation
#include<iostream.h>
#include<fstream.h>
#include<process.h>
#include<string.h>
#include<conio.h>
class student
{
private: char buf[45],name[25],sem[10],branch[10];
int pos;
public:
void read()
{
cout<<"name:"<<endl;
cin>>name;
cout<<"semester:"<<endl;
cin>>sem;
Page 12
cout<<"branch:"<<endl;
cin>>branch;
}
void pack(fstream &ofile)
{
read();
strcpy(buf,"");
strcat(buf,name);
strcat(buf,"|");
strcat(buf,sem);
strcat(buf,"|");
strcat(buf,branch);
strcat(buf,"|");
strcat(buf,"\n");
ofile.write(buf,strlen(buf));
}
void unpack(fstream &ifile)
{
char extra[45];
cout<<"Name \tSem \t Branch"<<endl;
while(!ifile.eof())
{
ifile.getline(name,25,'|');
ifile.getline(sem,10,'|');
ifile.getline(branch,10,'|');
ifile.getline(extra,45,'\n');
cout<<name<<"\t"<<sem<<"\t"<<branch<<endl;
}
}
Page 13
}
}
return 0;
}
void modify(fstream &ifile,char key[])
{
student s[10];
char extra[50];
int i=0;
while(!ifile.eof())
{
ifile.getline(s[i].name,25,'|');
ifile.getline(s[i].sem,10,'|');
ifile.getline(s[i].branch,10,'|');
ifile.getline(extra,45,'\n');
i++;
}
ifile.close();
int flag=0;
for(int j=0;j<i;j++)
{
if(strcmp(key,s[j].name)==0)
{
flag=1;
cout<<"record found details are:"<<endl;
cout<<s[j].name<<endl;
cout<<s[j].sem<<endl;
cout<<s[j].branch<<endl;
cout<<"enter the modificationdetails"<<endl;
cout<<"enetr the name"<<endl;
cin>>s[j].name;
cout<<"enter the sem;"<<endl;
cin>>s[j].sem;
cout<<"enter the branch"<<endl;
cin>>s[j].branch;
}
}
if(flag==0)
{
cout<<"Record not found\n";
return;
}
ifile.open("student.txt",ios::trunc|ios::app);
for(int k=0;k<i;k++)
Page 14
{
strcpy(buf,"");
strcat(buf,s[k].name);
strcat(buf,"|");
strcat(buf,s[k].sem);
strcat(buf,"|");
strcat(buf,s[k].branch);
strcat(buf,"|");
strcat(buf,"\n");
ifile.write(buf,strlen(buf));
}
}
};
void main()
{
int n,i,ch;
char key[10];
student stu;
//fstream ifile,ofile;
//ofile.open("student.txt",ios::trunc|ios::app);
//ofile.close();
for(;;)
{
clrscr();
cout<<"1.insert\n 2.display\n 3.search\n 4.modify\n 5.exit\n";
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
fstream ofile;
ofile.open("student.txt",ios::out|ios::cur|ios::app);
cout<<"enter the no of students";
cin>>n;
for(i=0;i<n;i++)
{
stu.pack(ofile);
}
ofile.close();
break;
case 2:
fstream infile;
infile.open("student.txt",ios::in);
stu.unpack(infile);
getch();
infile.close();
Page 15
break;
case 3:
cout<<"enter the record name to be searched"<<endl;
cin>>key;
fstream ifile;
ifile.open("student.txt",ios::in);
if(stu.search(ifile,key)==0)
cout<<"record not found\n";
getch();
ifile.close();
break;
case 4:
fstream iofile;
iofile.open("student.txt",ios::in|ios::out);
cout<<"enter the record name to be modified\n"<<endl;
cin>>key;
stu.modify(iofile,key);
getch();
iofile.close();
break;
default:exit(0);
}
}
}
Page 16
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 (relative
record number)
RRN is an ordinary number that gives the distance of current record from first record.
Using RRN, Direct access allows individual records to be read from different locations in
the file without reading intervening records.
When we are using fixed length record, we can calculate the byte offset of each record
using the fallowing formula
ByteOffset = (RRN - 1) × RecLen
RRN: relative record number(starts fron 0)
RecLen: size of fixed length record
//Implementation
#include<iostream.h>
#include<process.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
#define LEN 48
class student
{
private:char name[25],usn[11],age[3],buf[LEN];
public:
void pack()
{
strcpy(buf,usn);
strcat(buf,"|");
strcat(buf,name);
strcat(buf,"|");
strcat(buf,age);
for(int i=strlen(buf); i<LEN; i++) // LEN is the length of the record fixed
strcat(buf,"#");
}
void unpack()
{
strcpy(usn,strtok(buf,"|"));
strcpy(name,strtok(NULL,"|"));
strcpy(age,strtok(NULL,"#"));
}
void write_file(fstream &fout)
{
char rrn;
Page 17
};
void main()
{
student s;
int ch;
clrscr();
do
{
cout<<"\n1. Add a record\n2. Display a record\n3.Exit\n\nEnter choice: ";
cin>>ch;
switch(ch)
{
case 1:
fstream ofile;
// ofile=open("student4.txt",ios::out|ios::app);
s.write_file(ofile);
break;
case 2:
fstream ifile;
// ifile=open("student4.txt",ios::in);
Page 18
s.read_file(ifile);
break;
case 3:
exit(0);
break;
default:cout<<"Wrong choice!";
}
}while(ch!=3);
Page 19
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.
Index
A structure containing a set of entries, each consisting of a key field and a reference field,
Which is used to locate records in a data file.
Key field
Reference field
Simple index
Page 20
//Implementatio
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
int n=0,index=0;
class student
{
public: char name[20],usn[20],branch[5];
int sem;
void insert(fstream &f1,fstream &f2)
{
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter USN: ";
cin>>usn;
cout<<"Enter Sem: ";
cin>>sem;
cout<<"Enter Branch: ";
cin>>branch;
write(f1,f2);
}
void write(fstream &f1,fstream &f2)
{
f1<<++index<<"\t"<<usn<<"\n";
f2<<name<<"\t"<<usn<<"\t"<<sem<<"\t"<<branch<<"\n";
}
void display(fstream &f2)
{
f2>>name>>usn>>sem>>branch;
cout<<"Name \t USN \t SEM\t BRANCH \n";
cout<<name<<"\t"<<usn<<"\t"<<sem<<"\t"<<branch<<"\n";
}
int search(fstream &f1,char key[20])
{
int i,x;
for(i=1;i<=n;i++)
{
f1>>x>>usn;
if(strcmp(usn,key)==0)
return i;
}
cout<<"Record not found\n";
return 0;
Page 21
}
int remove(fstream &f1,char key[20])
{
int i;
i=search(f1,key);
return i;
}
};
void main()
{
fstream f1,f2;
student s[20],p;
int ch,k=0,i;
clrscr();
f1.open("m1.txt",ios::trunc);
f2.open("mn1.txt",ios::trunc);
f1.close();
f2.close();
for(;;)
{
cout<<"1.Insert 2.Display 3.Search 4.Delete 5.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: f1.open("m1.txt",ios::app);
f2.open("mn1.txt",ios::app);
cout<<"Enter no. of students: ";
cin>>k;
n=n+k;
for(int i=1;i<=k;i++)
s[i].insert(f1,f2);
f1.close();
f2.close();
break;
case 2: f2.open("mn1.txt",ios::in);
for(i=1;i<=n;i++)
s[i].display(f2);
f2.close();
break;
case 3: char usn[20];
cout<<"Enter USN to search: ";
cin>>usn;
f1.open("m1.txt",ios::in);
f2.open("mn1.txt",ios::in);
int j=p.search(f1,usn);
Page 22
if(j!=0)
{
cout<<"Record found & Details are\n";
cout<<"Name="<<s[j].name<<"\n"<<"USN="<<s[j].usn<<"\n";
cout<<"Sem="<<s[j].sem<<"\n"<<"Branch="<<s[j].branch<<"\n";
}
f1.close();
f2.close();
break;
case 4: f1.open("m1.txt",ios::in);
f2.open("mn1.txt",ios::in);
cout<<"Enter USN to delete: ";
cin>>usn;
j=p.remove(f1,usn);
if(j!=0)
{
for(i=j;i<n;i++)
s[i]=s[i+1];
cout<<"Deletion successfull\n";
}
n--;
index--;
f1.close();
f2.close();
f1.open("m1.txt",ios::trunc|ios::app);
f2.open("mn1.txt",ios::trunc|ios::app);
index=0;
for(i=1;i<=n;i++)
s[i].write(f1,f2);
f1.close();
f2.close();
break;
default:exit(0);
}
}
}
Page 23
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.
Concepts:
Key words:
//Implementation
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
int n=0,index=0;
class student
{
public: char name[20],usn[20],branch[5];
int sem;
void insert(fstream &f1,fstream &f2)
{
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter USN: ";
cin>>usn;
cout<<"Enter Sem: ";
cin>>sem;
cout<<"Enter Branch: ";
cin>>branch;
write(f1,f2);
}
void write(fstream &f1,fstream &f2)
Page 24
{
f1<<++index<<"\t"<<name<<"\n";
f2<<name<<"\t"<<usn<<"\t"<<sem<<"\t"<<branch<<"\n";
}
void display(fstream &f2)
{
f2>>name>>usn>>sem>>branch;
cout<<name<<"\t"<<usn<<"\t"<<sem<<"\t"<<branch<<"\n";
}
int search(fstream &f1,char key[20])
{
int i,x;
for(i=1;i<=n;i++)
{
f1>>x>>name;
if(strcmp(name,key)==0)
return i;
}
Page 25
case 1: f1.open("m.txt",ios::app);
f2.open("mn.txt",ios::app);
cout<<"Enter no. of students: ";
cin>>k;
n=n+k;
for(int i=1;i<=k;i++)
s[i].insert(f1,f2);
f1.close();
f2.close();
break;
case 2: f2.open("mn.txt",ios::in);
for(i=1;i<=n;i++)
s[i].display(f2);
f2.close();
break;
case 3: char name[20];
cout<<"Enter name to search: ";
cin>>name;
f1.open("m.txt",ios::in);
f2.open("mn.txt",ios::in);
int j=p.search(f1,name);
if(j!=0)
{
cout<<"Record found & Details are\n";
cout<<"Name="<<s[j].name<<"\n"<<"USN="<<s[j].usn<<"\n"
<<"Sem="<<s[j].sem<<"\n"<<"Branch="<<s[j].branch<<"\n";
}
f1.close();
f2.close();
break;
case 4: f1.open("m.txt",ios::in);
f2.open("mn.txt",ios::in);
cout<<"Enter name to delete: ";
cin>>name;
j=p.remove(f1,name);
if(j!=0)
{
for(i=j;i<n;i++)
s[i]=s[i+1];
cout<<"Deletion successfull\n";
}
n--;
index--;
f1.close();
f2.close();
f1.open("m.txt",ios::trunc|ios::app);
Page 26
f2.open("mn.txt",ios::trunc|ios::app);
index=0;
for(i=1;i<=n;i++)
s[i].write(f1,f2);
f1.close();
f2.close();
break;
default:exit(0);
}
}
}
Page 27
7. Write a C++ program to read two lists of names and then match the names in the two
lists using Consequential Match based on a single loop. Output the names common to both
the lists.
Cosequential Operations
Operations which involve accessing two or more input files sequentially and in parallel,
resulting in one or more output files produced by the combination of the input data.
Match
The process of forming a list containing all items common to two or more lists
Page 28
//Implementation
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.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\n";
getch();
exit(0);
}
cout<<"Enter the number of names you want to enter in file1 ";
cin>>m;
cout<<"\nEnter the names in assending order \n";
for(i=0;i<m;i++)
{
cin>>name;
out1<<name;
out1<<"\n";
}
cout<<"Enter the number of names you want to enter in file2 ";
cin>>n;
cout<<"\nEnter the names in assending order \n";
for(i=0;i<n;i++)
{
cin>>name;
out2<<name;
out2<<"\n";
}
out1.close();
out2.close();
}
Page 29
void main()
{
char list1[100][20],list2[100][20];
int i,j,m,n;
clrscr();
fstream out1,out2,out3;
writeLists();
out1.open("file1.txt",ios::in);
out2.open("file2.txt",ios::in);
out3.open("file3.txt",ios::out);
if((!out3)||(!out1)||(!out2))
{
cout<<"Unable to open one of the file";
getch();
exit(0);
}
m=0;
n=0;
while(!out1.eof())
{
out1.getline(list1[m],20,'\n');
cout<<list1[m]<<"\t";
m++;
}
cout<<endl;
while(!out2.eof())
{
out2.getline(list2[n],20,'\n');
cout<<list2[n]<<"\t";
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++;
Page 30
}
else if(strcmp(list1[i],list2[j])<0)
i++;
else
j++;
}
getch();
}
Page 31
8. Write a C++ program to read k Lists of names and merge them using kway merge
algorithm with k = 8.
Merge
The process of forming a list containing all items in any of two or more lists.
K-way merge
A merge of order k.
Order of a merge
//implementation
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
class record
{
public:
char name[20];
char usn[20];
}rec[20];
fstream file[8];
int no;
char fname[8][8]={"1.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
Page 32
{
f1.getline(recd[k].name,20,'|');
f1.getline(recd[k++].usn,20,'\n');
}
while(!f2.eof())
{
f2.getline(recd[k].name,20,'|');
f2.getline(recd[k++].usn,20,'\n');
}
int t,y;
record temp;
for(t=0;t<k-2;t++)
for(y=0;y<k-t-2;y++)
if(strcmp(recd[y].name,recd[y+1].name)>0)
{
temp=recd[y];
recd[y]=recd[y+1];
recd[y+1]=temp;
}
fstream temp1;
temp1.open(filename,ios::out);
for(t=1;t<k-1;t++)
temp1<<recd[t].name<<"|"<<recd[t].usn<<"\n";
f1.close();
f2.close();
temp1.close();
return;
}
void kwaymerge()
{
int i,k;
k=0;
char filename[7][20]={"11.txt","22.txt","33.txt","44.txt","111.txt","222.txt","1111.txt"};
for(i=0;i<8;i+=2)
{
merge_file(fname[i],fname[i+1],filename[k++]);
}
k=4;
for(i=0;i<4;i+=2)
{
merge_file(filename[i],filename[i+1],filename[k++]);
}
merge_file(filename[4],filename[5],filename[6]);
return;
}
Page 33
int main()
{
int i;
clrscr();
cout<<"enter no of records\n";
cin>>no;
cout<<"\nenter the details\n";
for(i=0;i<8;i++)
file[i].open(fname[i],ios::out);
for(i=0;i<no;i++)
{
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();
fstream result;
result.open("1111.txt",ios::in);
cout<<"sorted records\n";
char name[20],usn[20];
for(i=0;i<no;i++)
{
result.getline(name,20,'|');
result.getline(usn,20,'\n');
cout<<"\nName:"<<name<<"\nUsn:"<<usn<<"\n";
}
getch();
return 0;
}
Page 34
VIVA
Page 35