File Structures Laboratory 18isl67

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

File Structures Laboratory 18ISL67

aAlgorithm & Description of the program

Steps involved the program


Step1: Enter the number of names you want
Step2: Enter the names
Step3: Enter the names of the file where you want to write
Step4: Display each name in reverse order, on the screen
Step5: Write all the names to file1 using I/O redirection operator
Step6: Read the contents of the file1, reverse each name, and write to another file
Step7: Close file1 and file2

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.

Algorithm/Description of the Program


The functions used in the 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

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

void writeRecord() //Function to add record to file


{
fstream app;
student s;
int k,count;
app.open("student.txt",ios::out | ios::app); //Open file in append mode

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;

//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);
count = strlen(s.buffer);
int t = 45 - (count+2);
for(k=0 ; k < t ; k++)
{
strcat(s.buffer,"!");
}
strcat(s.buffer,"\n");
app<<s.buffer; //writing the packed information to buffer
app.close();
}

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;

//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<<"\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

Enter the choice: 1


Enter the student name = Asha
Enter the usn = 12
Enter the age = 21
Enter the sem =6
Enter the branch = ISE

0:exit
1:write to file

NIEIT, MysorePage 8
File Structures Laboratory 18ISL67

2:display the file


3:modify the file
4:search

Enter the choice: 2


Name Usn Age Sem Branch
Asha 12 21 6 ISE

0:exit
1:write to file
2:display the file
3:modify the file
4:search

Enter the choice3


Enter the usn12
The old values of the record with usn 12 are
name = Asha
usn = 12
age = 21
sem = 6
branch = ISE

Enter the new values


name = Asha H.B
usn = 12
age = 21
sem = 6
branch = ISE

0:exit
1:write to file
2:display the file
3:modify the file
4:search

Enter the choice4


Enter the record's usn you want to search = 12
Record found
AshaH.B 12 21 6 ISE

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.

Algorithm /Description of program


The functions used in the program
WriteRecord()
1) Open the file in append mode
2) Read student info from keyboard
3) Pack the student information fill the remaining free space in fixed length
record with !(symbol).
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

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

Name Usn Age Sem Branch


Meghana 12 21 6 CS

0:exit
1:write to file
2:display the file
3:modify the file
4:search

Enter the choice 3


Enter the usn12

The old values of the record with usn 12 are


name = Meghana
usn = 12
age = 21
sem = 6
branch = CS

Enter the new values


name = Megha
usn = 12
age = 21
sem = 6
branch = CS

NIEIT, MysorePage 15
File Structures Laboratory 18ISL67

0:exit
1:write to file
2:display the file
3:modify the file
4:search

Enter the choice2


Name Usn Age Sem Branch
Megha 12 21 6 CS

0:exit
1:write to file
2:display the file
3:modify the file
4:search

Enter the choice 4


Enter the record's usn you want to search = 12

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

Enter the choice = 1


Enter the student name = Ambika
Enter the usn = 23
Enter the age = 21
Enter the sem =6
Enter the branch = CS

0:exit
1:Insert
2:Search

Enter the choice = 1


Enter the student name = Darshan
Enter the usn = 24
Enter the age = 21
Enter the sem =6
Enter the branch = IS

0:exit
1:Insert
2:Search

Enter the choice = 2


Enter the RRN to be searched 2
Record found
Name Usn Age Sem Branch
Darshan 24 21 6 IS

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.

Algorithm/Description of the Program


Functions involved
In main()
1) Read the records
2) Display the record and pack
3) Write the record to a file
Retrieve_details()
1) Read the record
2) Unpack the recode
3) Display the record
Delete_record(char usno)
1) Read the records from file and unpacks
2) Compare with usn number if match delete record else return -1
3) Display the deleted record

#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 retrieve_details(char st_no[])


{
no = get_num_records();
for(int i=0;i<no;i++)
{
if(strcmp(stdrec[i].ind,st_no)==0)
{
cout<<"\n\n"<<"Student details : ";
out<<"\nUSN :"<<stdrec[i].usn<<"\
nName :"<<stdrec[i].name<<"\nAge :" <<stdrec[i].age<<"\nSem :"<<stdrec[i].sem<<"\
nBranch :"<<stdrec[i].branch<<"\n";
break;
}
}
}

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

cout<<"\nEnter the no. of students : ";


cin>>no;
cout<<"\nEnter the details :\n";
for(I=cnt; I<(cnt+no); I++)
{
cout<<"\nName : ";
cin>>s.name;
cout<<"\nAge : ";
cin>>s.age;
cout<<"\nUSN : ";
cin>>s.usn;
cout<<"\nSemester : ";
cin>>s.sem;
cout<<"\nBranch : ";
cin>>s.branch;
sprintf(buf2,"%s|%d\n",s.usn,I);
file1<<buf2;
sprintf(buf1,"%d|%s|%s|%s|%s|%s\n",I,s.usn,s.name,s.age,s.sem,s.branch);
file2<<buf1;
}
file1.close();
file2.close();
}

void search_record()
{
int I,flag1;
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 displayed\n";
cin>>st_usn;
file1.open("index.txt",ios::in);
if(!file1)
{
cout<<"\nError !\n";
exit(0);
}
flag1=0;
while(!file1.eof())
{
file1.getline(rt_usn,5,'|');
file1.getline(st_no,5,'\n');

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

void delete_stdrecord(char usno[])


{
int I=0;
fstream file1,file2;
no = get_num_records();
int flag=-1;
for(I=0;I<no;I++) //Check for the record's existence
{
if(strcmp(stdrec[I].ind,usno)==0)
{
flag=I;
break;
}
}
if(flag==-1) //Record not found
{
cout<<"\nError !\n";
return;
}
if(flag==(no-1)) //Delete found last record
{
no--;
cout<<"\nDeleted !\n";
}
else
{
for(I=flag;I<no;I++)
{
stdrec[I] = stdrec[I+1];
}
no--;
cout<<"\nDeleted !\n";
}
file1.open("index.txt",ios::out);

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

Enter the no. of students : 2


Enter the details :
Name : Sakshi
Age : 21
USN : 23
Semester : 6
Branch : EE

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

Please enter the USN of the student whose record is to be displayed


23

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 enter the USN of the student whose record is to be deleted


24
Deleted !

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.

Algorithm/Description of the Program

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 retrieve_record(char* ind)


{
int i,no;
no=get_num_records();
for(i=0;i<no;i++)
{
if(strcmp(ind,findex[i])==0)
{
cout<<"USN : "<<stdrec[i].usn<<"\nName :
"<<stdrec[i].name<<"\nAge : "<<stdrec[i].age<<"\nSem
:"<<stdrec[i].sem<<"\
nBranch :"<<stdrec[i].branch<<endl;
}
}
return;
}

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

void delete_record(char indx[])


{
int i,no,flag;
fstream file1,file2;

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

void sort_records(int no)


{
int i,j;
student temp;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(strcmp(stdrec[j].name,stdrec[j+1].name)>0)
{
temp=stdrec[j];
stdrec[j]=stdrec[j+1];
stdrec[j+1]=temp;
}
}
}
}

void create_indexfile(int no)


{
fstream file1,file2;
int i;
file1.open("secindex.txt",ios::out);
file2.open("record.txt",ios::out);

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

Enter the no. of students : 2


Enter the details :
Name : asha
Age : 23
USN : 12
Semester : 8
Branch : CS

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

Enter the no. of students : 1


Enter the details :
Name : vibha
Age : 24
USN : 14
Semester : 8
Branch : CS

Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
4

USN Name Age Sem Branch


12 asha 23 8 CS
13 vibha 23 8 IS
14 vibha 24 8 CS

Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
2

Please enter the name of the student whose record is to be displayed


vibha
Please choose the candidate's USN :
Name : vibha USN : 13
Name : vibha USN : 14
13

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

Please enter the name of the student whose record is to be deleted


vibha
Please choose the candidate's USN :
Name : vibha USN : 13
Name : vibha USN : 14
14

Deleted !

Please choose :
1:Add Record 2:Search Record 3:Delete Record 4:Display
Record
4

USN Name Age Sem Branch


12 asha 23 8 CS
13 vibha 23 8 IS

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.

Algorithm/Description of the program


Functions used in the program
writeLists()
1) Open two files in o/p mode
2) Read names from the keyboard and write the names to the file1 in ascending order
3) Repeat the step 2 to the file 2
4) Close file 1 and file 2
in main()
1) Open the file1, file2 and file3
2) Load file1 and file2.
3) Compare names in file1 and file2 on success display the names on screen as well as
write to file3
4) Close file1, file2 and file3
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int n,m,flag;
class variable
{
public: void writelist();
void match();
void display();
};
void variable::writelist()
{
fstream out1,out2;
int i;
char name[20];
out1.open("file1.txt",ios::out|ios::trunc);
out2.open("file2.txt",ios::out|ios::trunc);
cout<<"\nEnter count of names to be entered in file1: ";
cin>>m;
if(m>0)
cout<<"\nPlease enter names is Alphabetical order\n";
else
{
cout<<"\nInvalid no";
exit(0);
}

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.

Algorithm/Description of the program

1) Enter the number of records u want to read


2) Read the read record
3) Open files (8 files)
4) write one record to the each file file1, file2, and so on...file8
5) Start with once again to file1 to write records
6) Call kwaymerge() method
7) Write the result in 1111.txt

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

void merge_file(char *file1,char *file2,char *filename)


{
int k=0;
record recrd[50]; //temporary object(buffer) to hold data and size

NIEIT, MysorePage 41
File Structures Laboratory 18ISL67

fstream f1,f2; //big enough say more than 50.


f1.open(file1,ios::in);
f2.open(file2,ios::in);
while( !f1.eof() )
{
f1.getline(recrd[k].name,20,'|');
f1.getline(recrd[k++].usn,20,'\n');
}
while( !f2.eof() )
{
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++)
{
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);
for(t=1;t<k-1;t++)
temp1<<recrd[t].name<<"|"<<recrd[t].usn<<endl;
f1.close();
f2.close();
temp1.close();
return;
}

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

ENTER NAME : swaroop


Enter USN : 12
ENTER NAME : rakesh
Enter USN : 13
ENTER NAME : ramya
Enter USN : 14
ENTER NAME : divya
Enter USN : 15
ENTER NAME : dheeraj
Enter USN : 16
ENTER NAME : kavya
Enter USN : 17

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

You might also like