Fs Lab Manual
Fs Lab Manual
Fs Lab Manual
2018-2019
File Structures Laboratory with Mini Project
Laboratory Manual
15ISL68
Compiled by:
QUALITY POLICY
“We, at “Dayananda Sagar Academy of Technology and Management” are committed to
continually improve and deliver competitive quality technical education to the at most
satisfaction of student, their parents and potential employer.”
M2. Nurture team work in order to transform individual as a responsible leader and
entrepreneur for future trends.
M3. Inculcate research practices in teaching thus ensuring research blend among students.
M5. Inculcate the core Information Science and Engineering practices by providing advanced
technology laboratories.
PEO 1: Graduates shall have successful careers as Information Science Engineers and
will be able to lead & manage teams.
PEO 2: Graduates shall be professional in engineering practice and shall demonstrate
good problem solving, communication skills and contribute to address social issues.
PEO3: Graduates shall be pursuing advanced education, research in an excellent
environment which helps in the process of life-long learning.
PEO4: Graduates shall have robust knowledge in software architecture and its
application domains.
Program Outcomes (POs)
PO1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.
PO2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
PO3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate consideration
for the public health and safety, and the cultural, societal, and environmental considerations.
PO4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
PO8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive clear
instructions.
PO11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and leader
in a team, to manage projects and in multidisciplinary environments.
PO12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
\
DAYANANDA SAGAR ACADEMY OF TECHNOLOGY & MANAGEMENT
(Affiliated to Visvesvaraya Technological University,Belagavi & Approved by AICTE,New Delhi)
Opp. Art of Living, Udayapura, Kanakapura Road, Bangalore- 560082
DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING
(Accredited 3 years by NBA, New Delhi (Validity : 26-07-2018 to 30-06-2021)
SEMESTER: VI
Course Outcomes
At the end of the course the student will be able to:
CO2 Apply the concepts of file structure organization to produce the given application.
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<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
class student
{
public: char name[10];
} rec1[10],rec2[10];
int n;
void reads()
{
char name[10];
cout<<"enter the number of students:";
cin>>n;
cout<<"enter the student names:\n";
for(int i=0;i<n;i++)
{
cin>>rec1[i].name;
}
cout<<"reversed names\n";
for(i=0;i<n;i++)
{
strcpy(name,rec1[i].name);
strrev(name);
strcpy(rec2[i].name,name);
cout<<rec2[i].name<<"\n";
}
}
void write()
{
fstream file;
char fname[10];
cout<<"enter the filename\n";
cin>>fname;
file.open(fname,ios::out);
if(!file)
{
cout<<"could not open the file\n";
exit(1);
}
for(int i=0;i<n;i++)
{
file<<rec2[i].name<<"\n";
Dept. of ISE, DSATM 2018- 2019 1
File Structures Laboratory with Mini Project 15ISL68
}
}
void stored_names()
{
fstream f1,f2;
char fname1[10],fname2[10],name[10];
cout<<"enter the file from where you want to read\n";
cin>>fname1;
f1.open(fname1,ios::in);
if(!f1)
{
cout<<"could not open the file\n";
exit(1);
}
cout<<"enter the filename in which you want to store\n";
cin>>fname2;
f2.open(fname2,ios::out);
while(!f1.eof())
{
f1.getline(name,10,'\n');
strrev(name);
cout<<name<<"\n";
f2<<name<<"\n";
}
f1.close();
f2.close();
}
void main()
{
clrscr();
reads();
write();
stored_names();
getch();
}
OUTPUT:
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.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
class student
{
public: char name[10];
char usn[10];
char age[5];
char sem[5];
char branch[5];
char buffer[45];
};
fstream file;
student s;
void writerecord()
{
file.open("student.txt",ios::app);
if(!file)
{
cout<<"cannot open the file in append mode";
exit(1);
}
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);
int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
Dept. of ISE, DSATM 2018- 2019 4
File Structures Laboratory with Mini Project 15ISL68
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
file<<s.buffer; //writing the packed information to buffer
file.close();
}
void search()
{
char usn[10];
char extra[45];
file.open("student.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in read mode";
exit(0);
}
cout<<"\nenter the record's usn you want to search = ";
cin>>usn;
while(!file.eof())
{
file.getline(s.name,10,'|');
file.getline(s.usn,10,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,5,'!');
file.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nrecord found";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t";
cout<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
file.close();
getch();
return;
}
}
cout<<"\nrecord not found";
file.close();
getch();
}
void displayFile()
{
int i;
char extra[45];
file.open("student.txt",ios::in);
if(!file)
{
cout<<"\ncannot open the file in read mode";
getch();
exit(1);
}
i=0;
cout<<"\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n";
cout<<"----\t\t---\t\t---\t\t---\t\t------\n";
while(!file.eof())
{
file.getline(s.name,10,'|');
file.getline(s.usn,10,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,5,'!');
file.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++;
}
file.close();
getch();
}
void modify()
{
char usn[10];
char buffer[45];
char extra[45];
int i;
int j;
student s[20];
file.open("student.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in input mode";
getch();
exit(1);
}
cout<<"\nenter the usn of the record to be modified\n";
cin>>usn;
cout<<"\n";
i=0;
while(!file.eof())
{
file.getline(s[i].name,10,'|');
file.getline(s[i].usn,10,'|');
file.getline(s[i].age,5,'|');
file.getline(s[i].sem,5,'|');
file.getline(s[i].branch,5,'!');
file.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s[j].usn)==0)
{
cout<<"\nthe old values of the record with usn"<<usn<<"are";
cout<<"\nname = "<<s[j].name;
cout<<"\nusn = "<<s[j].usn;
cout<<"\nage = "<<s[j].age;
cout<<"\nsem = "<<s[j].sem;
cout<<"\nbranch = "<<s[j].branch;
if(j==i)
{
cout<<"\nthe record with usn " <<usn<< "is not present ";
getch();
return;
}
file.close();
file.open("student.txt",ios::out);
if(!file)
{
cout<<"\nunable to open the file in output mode";
getch();
return;
}
for(j=0;j<i;j++)
{
strcpy(buffer,s[j].name);
strcat(buffer,"|");
strcat(buffer,s[j].usn);
strcat(buffer,"|");
strcat(buffer,s[j].age);
strcat(buffer,"|");
strcat(buffer,s[j].sem);
strcat(buffer,"|");
strcat(buffer,s[j].branch);
int count=strlen(buffer);
for(int k=0;k<45-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
file<<buffer;
}
file.close();
}
void main()
{
int choice;
while(1)
{
clrscr();
cout<<"\n 0 : exit";
cout<<"\n 1 : write to file";
cout<<"\n 2 : display the file";
cout<<"\n 3 : modify the file";
cout<<"\n 4 : search";
cout<<"\n\n enter 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:
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.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
class student
{
public: char name[10];
char usn[10];
char age[5];
char sem[5];
char branch[5];
char buffer[100];
};
fstream file;
student s;
void writerecord()
{
file.open("program_3.txt",ios::app);
if(!file)
{
cout<<"cannot open the file in append mode";
getch();
exit(1);
}
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,"|");
Dept. of ISE, DSATM 2018- 2019 12
File Structures Laboratory with Mini Project 15ISL68
strcat(s.buffer,s.branch);
strcat(s.buffer,"\n");
file<<s.buffer;
file.close();
}
void search()
{
char usn[10];
char extra[45];
file.open("program_3.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in read mode";
getch();
exit(0);
}
cout<<"\nenter the record's usn you want to search = ";
cin>>usn;
while(!file.eof())
{
file.getline(s.name,10,'|');
file.getline(s.usn,10,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,5,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nrecord found";
cout<<"\nname\tusn\tage\tsem\tbranch";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t";
cout<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
file.close();
getch();
return;
}
}
cout<<"\nrecord not found";
file.close();
getch();
return;
}
void displayFile()
{
int i;
file.open("program_3.txt",ios::in);
if(!file)
{
cout<<"\ncannot open the file in read mode";
getch();
exit(1);
}
i=0;
printf("\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n");
while(!file.eof())
{
file.getline(s.name,15,'|');
file.getline(s.usn,15,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,5,'\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++;
}
file.close();
getch();
}
void modify()
{
char usn[10];
int i;
int j;
student s[100];
file.open("program_3.txt",ios::in);
if(!file)
{
cout<<"\nunable to open the file in input mode";
getch();
exit(1);
}
cout<<"\nenter the usn ";
cin>>usn;
i=0;
while(!file.eof())
{
file.getline(s[i].name,15,'|');
file.getline(s[i].usn,15,'|');
file.getline(s[i].age,5,'|');
file.getline(s[i].sem,5,'|');
file.getline(s[i].branch,5,'\n');
i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s[j].usn)==0)
{
cout<<"\nthe old values of the record with usn"<<usn<<"are";
cout<<"\nname = "<<s[j].name;
cout<<"\nusn = "<<s[j].usn;
cout<<"\nage = "<<s[j].age;
cout<<"\nsem = "<<s[j].sem;
cout<<"\nbranch = "<<s[j].branch;
if(j==i)
{
cout<<"\nthe record with usn " <<usn<< "is not present ";
getch();
return;
}
file.close();
file.open("program_3.txt",ios::out);
if(!file)
{
cout<<"\nunable to open the file in output mode";
getch();
return;
}
for(j=0;j<i;j++)
{
file<<s[j].name<<'|'<<s[j].usn<<'|'<<s[j].age
<<'|'<<s[j].sem<<'|'<<s[j].branch<<'\n';
}
file.close();
}
void main()
{
int choice;
while(1)
{
clrscr();
cout<<"\n 0 : exit";
cout<<"\n 1 : write to file";
cout<<"\n 2 : display the file";
cout<<"\n 3 : modify the file";
cout<<"\n 4 : search";
cout<<"\n\n enter 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);
OUTPUT:
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.
#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[10],age[5],sem[5],branch[15],buffer[100];
};
void writerecord()
{
fstream file;
student s;
int k,n;
file.open("program_4.txt",ios::app);
if(!file)
{
cout<<"\ncan not open the file in append mode\n";
getch();
exit(0);
}
printf("how many records\n");
scanf("%d",&n);
for(k=0;k<n;k++)
{
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;
file<<k<<"|"<<s.name<<"|"<<s.usn<<"|"
<<s.age<<"|"<<s.sem<<"|"<<s.branch<<"\n";
}
file.close();
}
void displayfile()
{
student s;
char rrn[10];
fstream file;
file.open("program_4.txt",ios::in);
if(!file)
{
cout<<"\ncannot open the file in input mode\n";
getch();
exit(1);
}
cout<<"\n";
printf("rrn\tname\t\tusn\t\tage\t\tsem\t\tbranch\n");
while(!file.eof())
{
file.getline(rrn,4,'|');
file.getline(s.name,15,'|');
file.getline(s.usn,15,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|');
file.getline(s.branch,15,'\n');
printf("\n%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",
rrn,s.name,s.usn,s.age,s.sem,s.branch);
}
file.close();
getch();
}
void search()
{
char rrn[10],rrn1[10][15];
int i;
student std[100];
cout<<"\n enter the rrn to be searched";
cin>>rrn;
fstream file;
file.open("program_4.txt",ios::in);
if(!file)
{
cout<<"\n can not open the file in input mode";
getch();
exit(0);
}
i=0;
printf("\n rrn\tname\tusn\tage\tsem\tbranch\n");
while(!file.eof())
{
file.getline(rrn1[i],4,'|');
file.getline(std[i].name,15,'|');
file.getline(std[i].usn,15,'|');
file.getline(std[i].age,5,'|');
file.getline(std[i].sem,5,'|');
file.getline(std[i].branch,15,'\n');
i++;
}
for(int j=0;j<i-1;j++)
{
if(strcmp(rrn,rrn1[j])==0)
{
printf("\n%s\t%s\t%s\t%s\t%s\t%s\n",
rrn,std[j].name,std[j].usn,std[j].age,
std[j].sem,std[j].branch);
printf("\n record found\n");
file.close();
return;
}
}
cout<<"\nrecord not found\n";
file.close();
return;
}
void main()
{
int choice;
clrscr();
while(1)
{
cout<<"\n 0:exit";
cout<<"\n 1:insert";
cout<<"\n 2:search";
cout<<"\n 3:display";
cout<<"\n enter the choice=";
cin>>choice;
switch(choice)
{
case 1:writerecord();
break;
case 2:search();
break;
case 3:displayfile();
break;
case 0:exit(0);
default:cout<<"\n invalid option";
break;
}
}
}
OUTPUT:
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<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
#include<iomanip.h>
class record
{
public: char age[5];
char usn[20];
char name[20];
char sem[2];
char branch[5];
}rec[20];
char st_no[5];
int no;
void retrieve_details()
{
fstream file2;
char name[20];
char age[5];
char usn[20];
char sem[2];
char branch[5];
char ind[5];
file2.open("record.txt",ios::in);
for(int i=0;i<no;i++)
{
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)
{
cout<<"\n\t"<<"student details :\n";
cout<<"\n\tUSN\tNAME\tAGE\tSEM\tBRANCH\n";
cout<<"\n\t"<<usn<<"\t"<<name<<"\t";
cout<<age<<"\t"<<sem<<"\t"<<branch<<"\n";
}
}
file2.close();
}
Dept. of ISE, DSATM 2018- 2019 25
File Structures Laboratory with Mini Project 15ISL68
file2.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file2.getline(ind,5,'|');
file2.getline(usn,20,'|');
file2.getline(name,20,'|');
file2.getline(age,5,'|');
file2.getline(sem,5,'|');
file2.getline(branch,5,'\n');
strcpy(rec[i].usn,usn);
strcpy(rec[i].name,name);
strcpy(rec[i].age,age);
strcpy(rec[i].sem,sem);
strcpy(rec[i].branch,branch);
}
int flag=-1;
for(i=0;i<no;i++)
{
if(strcmp(rec[i].usn,usno)==0)
{
flag=i;
}
}
if(flag==-1)
{
cout<<"error..! \n";
return;
}
if(flag==(no-1))
{
no--;
cout<<"record deleted !\n";
return;
}
for(i=flag;i<no;i++)
{
rec[i]=rec[i+1];
}
no--;
cout<<"\nrecord deleted !\n";
file2.close();
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
for(i=0;i<no;i++)
{
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 choice;
char rt_usn[20];
char st_usn[20];
char ind[2];
char name[20];
char age[2];
char sem[5];
char branch[5];
int i;
int flag;
int flag1;
clrscr();
file1.open("index.txt",ios::out);
file2.open("record.txt",ios::out);
if(!file1 || !file2)
{
cout<<"file creation error ! \n";
exit(0);
}
for(;;)
{
cout<<"\nenter the choice:\n\n";
cout<<"1 : add record\n";
cout<<"2 : search record\n";
cout<<"3 : delete record\n";
cout<<"4 : display record\n";
cout<<"5 : exit\n\n";
cin>>choice;
switch(choice)
{
case 1: cout<<"\nenter the no. of students : ";
cin>>no;
cout<<"\nenter 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;
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 4: cout<<"\n\tUSN\tNAME\tAGE\tSEM\tBRANCH\t\n";
for(i=0;i<no;i++)
{
cout<<"\n\t"<<rec[i].usn;
cout<<"\t"<<rec[i].name;
cout<<"\t"<<rec[i].age;
cout<<"\t"<<rec[i].sem;
cout<<"\t"<<rec[i].branch<<"\n";
}
break;
case 5: exit(0);
OUTPUT:
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 .
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
class record
{
public:
char age[5],sem[5],usn[20],name[20],branch[5];
}rec[20],found[20];
int no;
char st_no[5],rt_name[20];
void sortrecord()
{
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 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].name<<"|"<<rec[i].usn<<"|"<<
rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
index.close();
index2.close();
}
file.open("record.txt",ios::in);
for(int i=0;i<no;i++)
{
file.getline(ind,5,'|');
file.getline(name,20,'|');
file.getline(usn,20,'|');
file.getline(age,5,'|');
file.getline(sem,5,'|');
file.getline(branch,5,'\n');
if(strcmp(index,ind)==0)
{
cout<<"USN\tNAME\tAGE\tSEM\tBRANCH\n";
cout<<usn<<"\t"<<name<<"\t"<<age<<"\t"<<sem<<"\t"<<branch<<"\n";
}
}
file.close();
}
void retrieve_details()
{
fstream file;
char age[5],sem[5],usn[20],name[20],branch[5],ind[5];
char chusn[20],index[20][20];
file.open("secindex.txt",ios::in);
int k=0;
for(int 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<<"choose the candidates usn\n";
for(i=0;i<k;i++)
cout<<"USN:"<<found[i].usn<<"\tNAME:"<<found[i].name<<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;
}
file2.close();
file1.open("secindex.txt",ios::in);
file2.open("record.txt",ios::in);
for(i=0;i<no;i++)
{
file1<<rec[i].name<<"|"<<rec[i].usn<<"|"<<i<<"\n";
file2<<i<<"|"<<rec[i].name<<"|"<<rec[i].usn<<"|"<<
rec[i].age<<"|"<<rec[i].sem<<"|"<<rec[i].branch<<"\n";
}
file1.close();
file2.close();
}
}
file.close();
if(k==1)
{
delete_record(index[0]);
return;
}
else
{
cout<<"choose the candidates usn\n";
for(i=0;i<k;i++)
cout<<"USN:"<<found[i].usn<<" NAME:"<<found[i].name<<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;
char rt_usn[20],st_name[20],st_usn[20];
char age[5],sem[5],name[20],branch[5],ind[5];
int i,flag,flag1,choice;
clrscr();
for(;;)
{
cout<<"\n choose the option\n 1:add 2:search 3:delete 4:display 5:exit\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"enter the no of students\n";
cin>>no;
for(i=0;i<no;i++)
{
cout<<"enter the name:";
cin>>rec[i].name;
cout<<"usn:";
cin>>rec[i].usn;
cout<<"age:";
cin>>rec[i].age;
cout<<"sem:";
cin>>rec[i].sem;
cout<<"branch:";
cin>>rec[i].branch;
}
sortrecord();
indexfile();
break;
retrieve_details();
flag1=1;
}
}
if(!flag1)
cout<<"record search failed \n";
file1.close();
break;
case 4: cout<<"USN\tNAME\tAGE\tSEM\tBRANCH\n";
for(i=0;i<no;i++)
{
cout<<rec[i].usn<<"\t"<<rec[i].name<<"\t"<<rec[i].age<<"\t"
<<rec[i].sem<<"\t"<<rec[i].branch<<"\n";
}
break;
default:cout<<"invalid choice\n";
exit(0);
break;
}
}
}
OUTPUT:
7. Write a C++ program to read two lists of names and then match the names in the
two lists using Co-sequential Match based on a single loop. Output the names
common to both the lists.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<iostream.h>
#include<iomanip.h>
void writeLists()
{
fstream out1,out2;
int i;
int m;
int n;
char name[20];
out1.open("file1.txt",ios::out);
out2.open("file2.txt",ios::out);
void main()
{
Dept. of ISE, DSATM 2018- 2019 41
File Structures Laboratory with Mini Project 15ISL68
char list1[100][20];
char list2[100][20];
int i;
int j;
int m;
int 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);
clrscr();
m=0;
n=0;
printf("LIST-1 CONTENTS\n");
while( !out1.eof())
{
out1.getline(list1[m],20,'\n');
cout<<list1[m];
cout<<"\n";
m++;
}
printf("LIST-2 CONTENTS\n");
while( !out2.eof())
{
out2.getline(list2[n],20,'\n');
cout<<list2[n];
cout<<"\n";
n++;
}
m--;
n--;
i=0;
j=0;
cout<<"\nelements common to both files are\n";
out3<<list1[i];
cout<<list1[i]<<"\n";
out3<<'\n';
i++;
j++;
}
else if(strcmp(list1[i],list2[j])<0)
{
i++;
}
else
{
j++;
}
}
getch();
}
OUTPUT:
8. Write a C++ program to read k Lists of names and merge them using k-way merge
algorithm with k = 8.
#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"};
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;
}
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;
}
OUTPUT:
VIVA QUESTIONS
1. What is File Structure?
2. What is a File?
3. What is a field?
4. What is a Record?
5. What is fixed length record?
6. What is RRN?
7. What is Variable length record?
8. What are the different modes of opening a file?
9. What is ifstream()?
10. What is ofstream()?
11. What is the difference between read() and getline()?
12. How to close a file? What happens if a file is not closed?
13. What is Hashing? What is its use?
14. Explain any one collision resolution technique.
15. What is Btree? What is B+tree?
16. Differentiate between Logical and Physical file
17. What is the use of seekg() and seekp()?
18. Explain the different way of write data to a file.
19. Explain the different way of write data to a file.
20. What is meant by Indexing?
21. What is multilevel indexing?
22. What is File descriptor?
23. What is Fragmentation? What is internal fragmentation?
24. What is DMA?
25. What is a delimeter?
26. Define direct access.
27. Define sequential access.
28. What is the need of packing the record before writing into the file?
29. Explain ios::trunk and ios::nocreate
30. What is the use of End-of-file (EOF)?
31. What are stdin, stdout and stderr?
32. What is Fragmentation?.
33. What is data compression?
34. What are the properties of B tree?
35. How do we delete fixed length records?
36. How can we reclaim the deleted space dynamically?
37. What are the advantages and disadvantages of indexes that are too large to hold in
memory?
38. What is an AVL tree?
39. H M L B Q S T N A Z P E G C V J K D I U Show B tree creation, insertion,
splitting, deletion, merging and redistribution.
40. What is memset() ? Explain its parameters.