File Structures Laboratory 18isl67
File Structures Laboratory 18isl67
File Structures Laboratory 18isl67
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>
#include<string.h>
#include<fstream>
using namespace std;
int main()
{
char s1[25];
fstream file1,file2;
int i=0,j=0,x=0,c=0,kb=0;
char filename1[25],filename2[25];
cout<<endl<<"1 for standard i/o:"<<endl<<"2 for file i/o:"<<endl<<"enter your choice:" ;
cin>>kb;
switch(kb)
{
case 1: cout<<"enter name count:" ;
cin>>c;
for(j=1;j<=c;j++)
{
cout<<endl<<"enter name"<<j<<":" ;
cin>>s1;
x=strlen(s1);
cout<<"reversed name"<<j<<":" ;
for(i=x-1;i>=0;i--)
cout<<s1[i];
cout<<endl;
}
break;
case 2: cout<<"enter data filename:";
cin>>filename1;
cout<<"enter reversed data filename:";
NIEIT, MysorePage 1
File Structures Laboratory 18ISL67
cin>>filename2;
file1.open(filename1,ios::in);
file2.open(filename2,ios::out);
while(1)
{
file1.getline(s1,25,'\n');
if(file1.fail())
break;
x=strlen(s1);
for(i=x-1;i>=0;i--)
file2<<s1[i];
file2<<endl;
}
file1.close();
file2.close();
break;
}
}
Output:
Enter how many person details you want to enter
3
Enter the file name where it has to be written
correct.txt
Enter person details
divya
kumar
mohan
Enter the file name where output has to be written
reverse.txt
The details of the person in reverse that are entered is
ayvid
ramuk
nahom
NIEIT, MysorePage 2
File Structures Laboratory 18ISL67
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.
Search ()
1 ) Open the file in i/p mode
2 ) Load the file into main memory this includes unpacking of the records
3 ) Read the usn num you want to search
4 ) Search the record in the main memory which resembles the file contents
5 ) Close the file
DisplayFile()
1 ) Open the file in i/p mode
2 ) Load the file into main memory this includes unpacking of the records
3 ) Display loaded contents
4 ) Close the file
Modify()
1) Open the file in i/p mode
2) Load the file into main memory this includes unpacking of the records
3) Read the usn num you want to search
4) Search the record in the main memory on found modify the record.
5) Close file
6) Open the file in o/p mode write back to the file
7) Close
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class student
{
public:char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];
};
student s2[100];
NIEIT, MysorePage 3
File Structures Laboratory 18ISL67
if(!app)
{
cout<<"cannot open the file in output mode";
exit(0);
}
cout<<"\nEnter the student name = ";
cin>>s.name;
cout<<"\nEnter the usn = ";
cin>>s.usn;
cout<<"\nEnter the age = ";
cin>>s.age;
cout<<"\nEnter the sem = ";
cin>>s.sem;
cout<<"\nEnter the branch = ";
cin>>s.branch;
NIEIT, MysorePage 4
File Structures Laboratory 18ISL67
void search()
{
fstream in;
char usn[15],extra[45];
in.open("student.txt",ios::in);\
if(!in)
{
cout<<"\nUnable to open the file in input mode";
return;
}
cout<<"\nEnter the record's usn you want to search = ";
cin>>usn;
student s;
if(strcmp(s.usn,usn)==0)
{
cout<<"\n Record found";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
return;
}
}
cout<<"/n Record not found";
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";
NIEIT, MysorePage 5
File Structures Laboratory 18ISL67
return;
}
I=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
in.getline(extra,45,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
I++;
}
in.close();
}
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";
return;
}
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,'!');
in.getline(extra,45,'\n');
I++;
}
I--;
for(j=0;j<I;j++)
NIEIT, MysorePage 6
File Structures Laboratory 18ISL67
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname = "<<s1[j].name;
cout<<"\nusn = "<<s1[j].usn;
cout<<"\nage = "<<s1[j].age;
cout<<"\nsem = "<<s1[j].sem;
cout<<"\nbranch = "<<s1[j].branch;
cout<<"\nEnter the new values \n";
cout<<"\nname = "; cin>>s1[j].name;
cout<<"\nusn = "; cin>>s1[j].usn;
cout<<"\nage = "; cin>>s1[j].age;
cout<<"\nsem = "; cin>>s1[j].sem;
cout<<"\nbranch = "; cin>>s1[j].branch;
break;
}
}
if(j==I)
{
cout<<"\nThe record with usn "<<usn<<" is not present";
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout<<"\nUnable to open the file in output mode";
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);
count += 2;
for(int k=0; k < (45 - count); k++)
strcat(buffer,"!");
NIEIT, MysorePage 7
File Structures Laboratory 18ISL67
strcat(buffer,"\n");
out1<<buffer;
}
out1.close();
}
int main()
{
int choice;
for(;;)
{
cout<<"\n0:exit";
cout<<"\n1:write to file";
cout<<"\n2:display the file";
cout<<"\n3:modify the file";
cout<<"\n4:search";
cout<<"\n\nEnter the choice";
cin>>choice;
switch(choice)
{
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;
}
}
}
Output:
0:exit
1:write to file
2:display the file
3:modify the file
4:search
0:exit
1:write to file
NIEIT, MysorePage 8
File Structures Laboratory 18ISL67
0:exit
1:write to file
2:display the file
3:modify the file
4:search
0:exit
1:write to file
2:display the file
3:modify the file
4:search
NIEIT, MysorePage 9
File Structures Laboratory 18ISL67
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.
DisplayFile()
1) Open the file in i/p mode
2) Load the file into main memory this includes unpacking of the records
3) Display loaded contents
4) Close the file
Modify()
1) Open the file in i/p mode
2) Load the file into main memory this includes unpacking of the records
3) Read the usn num you want to search
4) Search the record in the main memory on found modifies the record.
5) Close file
6) Open the file in o/p mode write back to the file
7) Close
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
using namespace std;
class student
{
public:char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
NIEIT, MysorePage 10
File Structures Laboratory 18ISL67
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::out|ios::app);
if(!app)
{
cout<<"cannot open the file in output mode";
exit(0);
}
cout<<"\nEnter the student name = ";
cin>>s.name;
cout<<"\nEnter the usn = ";
cin>>s.usn;
cout<<"\nEnter the age = ";
cin>>s.age;
cout<<"\nEnter the sem = ";
cin>>s.sem;
cout<<"\nEnter the branch = ";
cin>>s.branch;
strcpy(s.buffer,s.name);
strcat(s.buffer,"|");
strcat(s.buffer,s.usn);
strcat(s.buffer,"|");
strcat(s.buffer,s.age);
strcat(s.buffer,"|");
strcat(s.buffer,s.sem);
strcat(s.buffer,"|");
strcat(s.buffer,s.branch);
strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
}
void search()
{
fstream in;
char usn[15],extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
exit(0);
}
cout<<"\nEnter the record's usn you want to search = ";
NIEIT, MysorePage 11
File Structures Laboratory 18ISL67
cin>>usn;
student s;
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<<"\nname\tusn\tage\tsem\tbranch";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
return;
}
}
cout<<"\nRecord not found";
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";
exit(0);
}
I=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\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();
}
NIEIT, MysorePage 12
File Structures Laboratory 18ISL67
void modify()
{
fstream in;
char usn[15];
int I,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout<<"\nUnable to open the file in input mode";
exit(0);
}
cout<<"\nEnter the usn";
cin>>usn;
I=0;
while(!in.eof())
{
in.getline(s1[I].name,15,'|');
in.getline(s1[I].usn,15,'|');
in.getline(s1[I].age,5,'|');
in.getline(s1[I].sem,5,'|');
in.getline(s1[I].branch,15,'\n');
I++;
}
I--;
for(j=0;j<I;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout<<"\nThe old values of the record with usn "<<usn<<" are ";
cout<<"\nname = "<<s1[j].name;
cout<<"\nusn = "<<s1[j].usn;
cout<<"\nage = "<<s1[j].age;
cout<<"\nsem = "<<s1[j].sem;
cout<<"\nbranch = "<<s1[j].branch;
cout<<"\nEnter the new values \n";
cout<<"\nname = "; cin>>s1[j].name;
cout<<"\nusn = "; cin>>s1[j].usn;
cout<<"\nage = "; cin>>s1[j].age;
cout<<"\nsem = "; cin>>s1[j].sem;
cout<<"\nbranch = "; cin>>s1[j].branch;
break;
}
}
if(j==I)
NIEIT, MysorePage 13
File Structures Laboratory 18ISL67
{
cout<<"\nThe record with usn "<<usn<<" is not present";
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout<<"\nUnable to open the file in output mode";
return;
}
student s;
for(j=0;j<I;j++)
{
out1<<s1[j].name<<'|'<<s1[j].usn<<'|'<<s1[j].age<<'|'<<s1[j].sem<<'|'<<s1[j].branch<<'\n';
}
out1.close();
}
int main()
{
int choice;
for(;;)
{
cout<<"\n0:exit";
cout<<"\n1:write to file";
cout<<"\n2:display the file";
cout<<"\n3:modify the file";
cout<<"\n4:search";
cout<<"\n\nEnter the choice";
cin>>choice;
switch(choice)
{
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;
}
}
}
NIEIT, MysorePage 14
File Structures Laboratory 18ISL67
Output:
0:exit
1:write to file
2:display the file
3:modify the file
4:search
Enter the choice 1
Enter the student name = Meghana
Enter the usn = 12
Enter the age = 21
Enter the sem =6
Enter the branch = CS
0:exit
1:write to file
2:display the file
3:modify the file
4:search
Enter the choice 2
0:exit
1:write to file
2:display the file
3:modify the file
4:search
NIEIT, MysorePage 15
File Structures Laboratory 18ISL67
0:exit
1:write to file
2:display the file
3:modify the file
4:search
0:exit
1:write to file
2:display the file
3:modify the file
4:search
Record found
name usn age sem branch
Megha 12 21 6 CS
NIEIT, MysorePage 16
File Structures Laboratory 18ISL67
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.
Algorithm/Description of Program
Functions used in this program
WriteRecord()
1) Open the file in append mode
2) Read student info from keyboard
3) Pack the student information
4) Write in to the file
5) Close the file
Search ()
1) Open the file in i/p mode
2) Load the file into main memory this includes unpacking of the records
3) Read the usn num you want to search
4) Search the record in the main memory which resembles the file contents
5) Close the file
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<fstream>
using namespace std;
class student
{
public:char rrn[5],name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s[100];
int getNumRecords()
{
fstream in;
int cnt=0;
in.open("student.txt",ios::in);
if(!in)
{
//cout<<"\ncannot open the file in output mode";
return 0;
}
while(!in.eof())
{
in.getline(s[cnt].rrn,5,'|');
in.getline(s[cnt].name,15,'|');
in.getline(s[cnt].usn,15,'|');
in.getline(s[cnt].age,5,'|');
in.getline(s[cnt].sem,5,'|');
NIEIT, MysorePage 17
File Structures Laboratory 18ISL67
in.getline(s[cnt].branch,15,'\n');
cnt++;
}
cnt--;
//returning number of records
return cnt;
}
void search()
{
char rrn[5];
int cnt=0;
cout<<"\nEnter the RRN to be searched ";
cin>>rrn;
cnt=getNumRecords();
printf("Name\tUsn\tAge\tSem\tBranch\n");
for(int j=0; j < cnt; j++)
{
if(strcmp(rrn,s[j].rrn)==0)
{
printf("\nRecord found");
printf("\n%s\t%s\t%s\t%s\t%s",s[j].name,s[j].usn,s[j].age,s[j].sem,s[j].branch);
return;
}
}
cout<<"\nRecord not found";
return;
}
void writeRecord()
{
fstream app;
student s;
char rcnt[5];
int cnt;
app.open("student.txt",ios::out|ios::app);
if(!app)
{
cout<<"cannot open the file in output mode";
exit(0);
}
cout<<"\nEnter the student name = ";
cin>>s.name;
cout<<"\nEnter the usn = ";
cin>>s.usn;
cout<<"\nEnter the age = ";
NIEIT, MysorePage 18
File Structures Laboratory 18ISL67
cin>>s.age;
cout<<"\nEnter the sem = ";
cin>>s.sem;
cout<<"\nEnter the branch = ";
cin>>s.branch;
cnt = getNumRecords();
cnt++;
sprintf(rcnt,"%d",cnt);
strcpy(s.buffer,rcnt);
strcat(s.buffer,"|");
strcat(s.buffer,s.name);
strcat(s.buffer,"|");
strcat(s.buffer,s.usn);
strcat(s.buffer,"|");
strcat(s.buffer,s.age);
strcat(s.buffer,"|");
strcat(s.buffer,s.sem);
strcat(s.buffer,"|");
strcat(s.buffer,s.branch);
strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
}
int main()
{
int choice;
for(;;)
{
cout<<"\n0:exit";
cout<<"\n1:Insert";
cout<<"\n2:Search";
cout<<"\nEnter the choice = ";
cin>>choice;
switch(choice)
{
case 1:writeRecord();break;
case 2:search();break;
case 0:exit(0);
default:cout<<"\nInvalid option";
}
}
}
NIEIT, MysorePage 19
File Structures Laboratory 18ISL67
Output:
0:exit
1:Insert
2:Search
0:exit
1:Insert
2:Search
0:exit
1:Insert
2:Search
NIEIT, MysorePage 20
File Structures Laboratory 18ISL67
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.
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
//Record specification
class student
{
public: char ind[5],usn[5],name[15],age[5],sem[5],branch[15];
}stdrec[20];
int no;
int get_num_records()
{
int I=0;
fstream file2;
file2.open("record.txt",ios::in);
if(!file2)
{
return 0;
}
while(!file2.fail()) //Unpacking record data
{
file2.getline(stdrec[I].ind,5,'|');
file2.getline(stdrec[I].usn,5,'|');
file2.getline(stdrec[I].name,15,'|');
NIEIT, MysorePage 21
File Structures Laboratory 18ISL67
file2.getline(stdrec[I].age,5,'|');
file2.getline(stdrec[I].sem,5,'|');
file2.getline(stdrec[I].branch,15,'\n');
I++;
}
I--;
file2.close();
return I;
}
void add_record()
{
char buf1[100],buf2[20];
fstream file1,file2;
int I,cnt;
student s;
cnt=get_num_records();
file1.open("index.txt",ios::out|ios::app);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
file2.open("record.txt",ios::out|ios::app);
if(!file2)
{
cout<<"\nError !\n";
exit(0);
}
NIEIT, MysorePage 22
File Structures Laboratory 18ISL67
void search_record()
{
int I,flag1;
char st_no[5],st_usn[5],rt_usn[5];
fstream file1;
NIEIT, MysorePage 23
File Structures Laboratory 18ISL67
if(strcmp(st_usn,rt_usn)==0)
{
retrieve_details(st_no);
flag1=1;
break;
}
}
if(!flag1)
cout<<"\nRecord search failed !!\n";
file1.close();
}
NIEIT, MysorePage 24
File Structures Laboratory 18ISL67
file2.open("record.txt",ios::out);
for(I=0;I<no;I++)
{
file1<<stdrec[I].usn<<"|"<<I<<"\n";
file2<<I<<"|"<<stdrec[I].usn<<"|"<<stdrec[I].name<<"|"<<stdrec[I].age<<"|"<<stdrec[I].sem<<
"|"<<stdrec[I].branch<<"\n";
}
file1.close();
file2.close();
return;
}
void delete_record()
{
int I,flag;
char st_no[5],st_usn[5],rt_usn[5];
fstream file1;
cout<<"\nPlease enter the USN of the student ";
cout<<" whose record is to be deleted\n";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
flag=0;
while(!file1.eof())
{
file1.getline(rt_usn,5,'|'); //Search index file and
file1.getline(st_no,5,'\n'); //call deletion
//if index found
if(strcmp(st_usn,rt_usn)==0)
{
delete_stdrecord(st_no);
flag=1;
break;
}
}
if(!flag)
cout<<"\nDeletion failed !\n";
file1.close();
}
NIEIT, MysorePage 25
File Structures Laboratory 18ISL67
void display_records()
{
fstream file2;
student s;
file2.open("record.txt",ios::in);
cout<<"\n\n"<<"Student details : \n";
Cout<<"Index"<<"\t"<<"USN"<<"\t"<<"Name"<<"\t"<<"Age"<<"\t"<<"Sem"<<"\
t"<<"Branch"<<"\t"<<endl;
while(!file2.fail()) //Unpacking record data
{
file2.getline(s.ind,5,'|');
file2.getline(s.usn,5,'|');
file2.getline(s.name,15,'|');
file2.getline(s.age,5,'|');
file2.getline(s.sem,5,'|');
file2.getline(s.branch,15,'\n');
cout<<s.ind<<"\t"<<s.usn<<"\t"<<s.name<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch<<"\n";
}
file2.close();
}
int main()
{
int choice;
for(;;)
{
cout<<"\nPlease choose :\n 1:Add Record\n";
cout<<"2:Search Record\n 3:Delete Record\n";
cout<<"4:Display Record\n";
cin>>choice;
switch(choice)
{
case 1: add_record(); break;
case 2: search_record(); break;
case 3: delete_record(); break;
case 4: display_records();break;
default:cout<<"\nInvalid choice !\n"; exit(0);
}
NIEIT, MysorePage 26
File Structures Laboratory 18ISL67
Output:
Please choose :
1:Add Record
2:Search Record
3:Delete Record
4:Display Record
1
Name : Suhas
Age : 21
USN : 24
Semester : 6
Branch : EC
Please choose :
1:Add Record
2:Search Record
3:Delete Record
4:Display Record
4
Student details :
Index USN Name Age Sem Branch
0 23 Sakshi 21 6 EE
1 24 Suhas 21 6 EC
Please choose :
1:Add Record
2:Search Record
3:Delete Record
4:Display Record
2
NIEIT, MysorePage 27
File Structures Laboratory 18ISL67
Student details :
USN :23
Name :Sakshi
Age :21
Sem :6
Branch :EE
Please choose :
1:Add Record
2:Search Record
3:Delete Record
4:Display Record
3
Please choose :
1:Add Record
2:Search Record
3:Delete Record
4:Display Record
4
Student details :
Index USN Name Age Sem Branch
0 23 Sakshi 21 6 EE
NIEIT, MysorePage 28
File Structures Laboratory 18ISL67
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.
Functions involved
sortrecords()
sort according to the usn number
deleterecord()
1) Open files a in read mode
2) Read the record and unpack the record
3) Find the index if found delete else -1
4) Close file
delete_index()
Find the what is index of the record is there
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class student
{
public: char name[15],usn[5],age[5],sem[5],branch[15];
}stdrec[20],found[20];
char st_no[5],rt_name[20];
char findex[20][5];
int get_num_records()
{
fstream file;
int i=0;
file.open("record.txt",ios::in);
if(!file)
{
return 0;
}
while(!file.eof())
{
file.getline(findex[i],5,'|');
file.getline(stdrec[i].usn,5,'|');
file.getline(stdrec[i].name,15,'|');
file.getline(stdrec[i].age,5,'|');
file.getline(stdrec[i].sem,5,'|');
file.getline(stdrec[i].branch,15,'\n');
NIEIT, MysorePage 29
File Structures Laboratory 18ISL67
i++;
}
i--;
file.close();
return i;
}
void search_records()
{
int k=0,i,no;
char name[15],usn[5],ind[5],chusn[5],resind[5][20];
fstream file;
file.open("secindex.txt",ios::in);
cout<<"Please enter the name of the student whose record is to be displayed\n";
cin>>rt_name;
while(!file.eof())
{
file.getline(name,15,'|');
file.getline(usn,5,'|');
file.getline(ind,5,'\n');
if(strcmp(name,rt_name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(resind[k],ind);
k++;
}
}
file.close();
NIEIT, MysorePage 30
File Structures Laboratory 18ISL67
if(k==1)
{
retrieve_record(resind[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(resind[i]);
return;
}
}
cout<<"Invalid Entry !\n";
return;
}
no=get_num_records();
flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(findex[i],indx)==0)
flag=i;
}
if(flag==-1)
{
cout<<"Error !\n";
return;
}
if(flag==(no-1))
{
no--;
cout<<"Deleted !\n";
}
NIEIT, MysorePage 31
File Structures Laboratory 18ISL67
else
{
for(i=flag;i<no;i++)
{
stdrec[i]=stdrec[i+1];
}
no--;
cout<<"Deleted !\n";
}
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
file1<<stdrec[i].name<<"|"<<stdrec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<stdrec[i].usn<<"|"<<stdrec[i].name<<"|"<<stdrec[i].age<<"|"<<stdrec[i].sem<<"|
"<<stdrec[i].branch<<"\n";
}
file2.close();
file1.close();
return;
}
void remove_record()
{
fstream file;
int i,k=0,no;
char name[15],usn[5],ind[5],chusn[5],resind[20][5],st_name[15];
cout<<"Please enter the name of the student whose record is to be deleted\n";
cin>>st_name;
file.open("secindex.txt",ios::in);
while(! file.eof())
{
file.getline(name,15,'|');
file.getline(usn,5,'|');
file.getline(ind,5,'\n');
if(strcmp(st_name,name)==0)
{
strcpy(found[k].name,name);
strcpy(found[k].usn,usn);
strcpy(resind[k],ind);
k++;
}
}
file.close();
if(k==1)
{
NIEIT, MysorePage 32
File Structures Laboratory 18ISL67
delete_record(resind[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(resind[i]);
return;
}
}
cout<<"Invalid Entry !\n";
return;
}
NIEIT, MysorePage 33
File Structures Laboratory 18ISL67
for(i=0;i<no;i++)
{
file1<<stdrec[i].name<<"|"<<stdrec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<stdrec[i].usn<<"|"<<stdrec[i].name<<"|"<<stdrec[i].age<<"|"<<stdrec[i].sem<<"|
"<<stdrec[i].branch<<"\n";
}
file1.close();
file2.close();
}
void add_records()
{
int i,cnt,no;
cout<<"\nEnter the no. of students : ";
cin>>no;
cout<<"\nEnter the details :\n";
cnt = get_num_records();
for(i=cnt;i<(cnt+no);i++)
{
cout<<"\nName : ";
cin>>stdrec[i].name;
cout<<"\nAge : ";
cin>>stdrec[i].age;
cout<<"\nUSN : ";
cin>>stdrec[i].usn;
cout<<"\nSemester : ";
cin>>stdrec[i].sem;
cout<<"\nBranch : ";
cin>>stdrec[i].branch;
}
sort_records((cnt+no));
create_indexfile((cnt+no));
}
void display_records()
{
student s;
fstream file;
int i=0;
file.open("record.txt",ios::in);
if(!file)
{
cout<<"Error\n";
return;
}
cout<<"\nUSN\tName\tAge\tSem\tBranch\n";
NIEIT, MysorePage 34
File Structures Laboratory 18ISL67
while(!file.eof())
{
file.getline(findex[i],5,'|');
file.getline(s.usn,5,'|');
file.getline(s.name,15,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,15,'\n');
cout<<s.usn<<"\t"<<s.name<<"\t"<<s.age<<"\t"<<s.sem<<"\t"<<s.branch<<"\t"<<endl;
}
file.close();
return;
}
int main()
{
int choice;
for(;;)
{
cout<<"Please choose :\n1:Add Record 2:Search Record
3:Delete Record 4:Display Record\n";
cin>>choice;
switch(choice)
{
case 1: add_records(); break;
case 2: search_records(); break;
case 3: remove_record(); break;
case 4: display_records(); break;
default: cout<<"Invalid choice !\n"; exit(0); break;
}
}
}
Output:
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
1
NIEIT, MysorePage 35
File Structures Laboratory 18ISL67
Name : vibha
Age : 23
USN : 13
Semester : 8
Branch : IS
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
1
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
4
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
2
USN : 13
Name : vibha
Age : 23
Sem :8
Branch :IS
NIEIT, MysorePage 36
File Structures Laboratory 18ISL67
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
3
Deleted !
Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
4
NIEIT, MysorePage 37
File Structures Laboratory 18ISL67
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.
NIEIT, MysorePage 38
File Structures Laboratory 18ISL67
for(i=0;i<m;i++)
{
cout<<"\nEnter name: ";
cin>>name;
out1<<name<<endl;
}
cout<<"\nEnter count of names to be entered in file2: ";
cin>>n;
if(n<=0)
{
cout<<"\nInvalid no";
exit(0);
}
for(i=0;i<n;i++)
{
cout<<"\nEnter name: "; cin>>name;
out2<<name<<endl;
}
out1.close();
out2.close();
}
void variable::match()
{
char list1[100][20],list2[100][20];
int i,j;
fstream in1,in2;
in1.open("file1.txt",ios::in);
i=0; j=0;
in2.open("file2.txt",ios::in);
fstream out1;
out1.open("file3.txt",ios::out|ios::trunc);
while(i<m)
{
in1.getline(list1[i++],20,'\n');
}
while(j<n)
{
in2.getline(list2[j++],20,'\n');
}
i=j=0;
while(i<m && j<n)
{
if(strcmp(list1[i],list2[j])==0)
{
out1<<list1[i]<<endl;
flag=1; i++;j++;
NIEIT, MysorePage 39
File Structures Laboratory 18ISL67
}
else if(strcmp(list1[i],list2[j])<0)
i++;
else
j++;
}
in1.close();
in2.close();
out1.close();
}
void variable::display()
{
fstream in1;
char name[20];
in1.open("file3.txt",ios::in);
if(flag)
{
cout<<"\nMatched list\n";
while(1)
{
in1.getline(name,20,'\n');
if(in1.eof()) break;
cout<<name<<endl;
}
}
else cout<<"\nNo matching strings";
}
int main()
{
variable v;
v.writelist();
v.match();
v.display();
return 0;
}
Output:
Enter count of names to be entered in file1: 3
Please enter names is Alphabetical order
Enter name: Abha
Enter name: Bhanu
Enter name: Suhas
Enter count of names to be entered in file2: 3
Enter name: Bhanu
Enter name: Ramya
Enter name: Suhas
Matched list
NIEIT, MysorePage 40
File Structures Laboratory 18ISL67
Bhanu, Suhas
8) Write a C++ program to read k Lists of names and merge them using k way merge
algorithm with k = 8.
kwaymerge()
1) Merge and sort the 8 original files onto the four files indicated {11.txt, 22.txt....}
2) By calling mege_file()
3) Merge and sort the four files onto 111.txt and 222.txt
4) By calling mege_file()
5) Merge and sort the two files onto the 1111.txt file 6) by calling merge_file()
merge_file()
1) Open the two files in read mode
2) Arrange them in order
3) Write to another file
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class record
{
public: char name[20];
char usn[20];
}rec[20];
int no;
fstream file[8];
char fname[8][8]={"1.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
NIEIT, MysorePage 41
File Structures Laboratory 18ISL67
void kwaymerge()
{
char filename[7][20]={"11.txt","22.txt","33.txt","4.txt","111.txt","222.txt","1111.txt"};
int i; int k=0;
for(i=0;i<8;i+=2)
{
merge_file(fname[i],fname[i+1],filename[k++]);
}
NIEIT, MysorePage 42
File Structures Laboratory 18ISL67
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]);
}
int main()
{
int i;
cout<<"ENter NO. of records:\t";
cin>>no;
cout<<"\nEnter details:\n";
for(i=0;i<8;i++)
file[i].open(fname[i],ios::out);
for(i=0;i<no;i++)
{
cout<<"\nENTER NAME :\t";
cin>>rec[i].name;
cout<<"\nEnter USN :\t";
cin>>rec[i].usn;
file[i%8]<<rec[i].name<<"|"<<rec[i].usn<<endl;
}
for(i=0;i<8;i++)
file[i].close();
kwaymerge();
fstream result;
result.open("1111.txt",ios::in);
cout<<"Sorted records\n";
char name1[20],usn1[20];
for(i=0;i<no;i++)
{
result.getline(name1,20,'|');
result.getline(usn1,20,'\n');
cout<<"NAME :"<<name1<<"\nUSN :"<<usn1<<endl;
}
return 0;
}
Output:
ENter NO. of records: 8
Enter details:
ENTER NAME : bhavya
Enter USN : 10
ENTER NAME : suhas
Enter USN : 11
NIEIT, MysorePage 43
File Structures Laboratory 18ISL67
Sorted records
NAME :bhavya
USN :10
NAME :dheeraj
USN :16
NAME :divya
USN :15
NAME :kavya
USN :17
NAME :rakesh
USN :13
NAME :ramya
USN :14
NAME :suhas
USN :11
NAME :swaroop
USN :12
NIEIT, MysorePage 44
File Structures Laboratory 18ISL67
NIEIT, MysorePage 45