Documente Academic
Documente Profesional
Documente Cultură
BNM Institute of
Technology
(Approved by AICTE & Affiliated to VTU, Karnataka)
(An ISO 9001:2008 Certified Institution)
Post Box No, 7087, 27th cross, 12th Main Road,
Banshankari 2nd Stage, Bengalore-560070
Phone: 91-8026711781/26711782
Fax: 91-80-26710881,E-mail:bnmitprincipal@gmail.com
Visit us at: www.bnmit.org
VTU LAB SYLLABUS
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.
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.
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.
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.
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.
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.
8. Write a C++ program to read k Lists of names and merge them using k-way merge
algorithm with k = 8.
9. Write a C++ program to implement B-Tree for a given set of integers and its
operations insert ( ) and search ( ). Display the tree.
10. Write a C++ program to implement B+ tree for a given set of integers and its
operations insert ( ), and search ( ). Display the tree.
11. Write a C++ program to store and retrieve student data from file using hashing. Use
any collision resolution technique.
12. Write a C++ program to reclaim the free space resulting from the deletion of
records using linked lists.
Contents
VTU LAB SYLLABUS......................................................................................2
Contents...................................................................................................... 3
Program 2.................................................................................................... 9
Program 3.................................................................................................. 19
Program 4.................................................................................................. 27
Program 5.................................................................................................. 33
Program 6.................................................................................................. 42
Program 7.................................................................................................. 58
Progrm 8.................................................................................................... 63
Program 9.................................................................................................. 68
Program 10................................................................................................79
Program 11................................................................................................93
Program 12..............................................................................................100
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<process.h>
#include<string.h>
#include<fstream.h>
class names
{
public:char name[50];
};
void reverse(ofstream &out,char name[255])
{
char *rev;
rev=name+strlen(name)-1;
while(rev>=name)
{
cout<<*rev;
out<<*rev;
rev--;
}
cout<<endl;
out<<"\n";
Enter name
sohan
The name in reverse order
nahos
Enter name
dilip
The name in reverse order
pilid
The names from files (f1.txt)
priya
padma
ajit
soni
dilip
Reverse order from files (f2.txt)
ayirp
amdap
tija
nahos
pilid
Write a C++ program to read and write and student objects with fixed length
records and the fields delimited by “|”.implement pack(),unpack(),modify() and
search() methods.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<string.h>
class student
{
public:char name[25],usn[15],branch[15],buffer[45];
};
student s,s1[100];
char extra[45];
int i,no=0,mode=0;
void pack()
{
fstream app;
if(mode==0)
app.open("st1.txt",ios::app);
else
app.open("st1.txt",ios::out);
if(!app)
{
cout<<"cant open the file in output mode";
BNMIT/ISE/2012/VI/ File Structure laboratory Page 9
getch();
exit(0);
}
strcpy(s.buffer,s.name);
strcat(s.buffer,"|");
strcat(s.buffer,s.usn);
strcat(s.buffer,"|");
strcat(s.buffer,s.branch);
int count=strlen(s.buffer);
for(int k=0;k<45-count;k++)
strcat(s.buffer,"|");
strcat(s.buffer,"\n");
app<<s.buffer;
app.close();
}
void unpack()
{
fstream in;
in.open("st1.txt",ios::in);
i=0,no=0;
if(!in)
{
cout<<"cant open the file in input mode";
getch();
exit(0);
}
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
Output:
0: Exit
1: write
2: Display
3: Modify
4: Search
Enter your choice
1
Enter the student name
Karthik
Enter the student USN
1bg09is015
Enter the student branch
Ise
0: Exit
1: write
2: Display
3: Modify
4: Search
Enter your choice
1
Enter the student name
Dhanvi
Enter the student USN
1bg09is007
Write a C++ program to read and write and student objects with variable length
records using any suitable record structure. Implement pack(),unpack(),modify()
and search() methods.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<string.h>
class student
{
public:char name[25],usn[15],branch[15],buffer[45];
};
student s,s1[100];
char extra[45];
int i,no=0,mode=0;
void pack()
{
fstream app;
if(mode==0)
app.open("st2.txt",ios::app);
else
app.open("st2.txt",ios::out);
if(!app)
{
cout<<"cant open the file in output mode";
Program 4
Write a c++ program to write student objects with variable-length records using
any sitable record structure and to read from this file a student record using RRN.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
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<conio.h>
#include<string.h>
#include<iostream.h>
#include<process.h>
#include<values.h>
#include<fstream.h>
#include<stdlib.h>
class record
{
public:char name[20],usn[20],branch[20],ind[5];
}rec[20];
char st_no[5];
int no;
void pack()
{
fstream fle1,fle2;
fle1.open("i.txt",ios::out);
fle2.open("r.txt",ios::out);
if((!fle1)||(!fle2))
{
cout<<"cant open either\n";
getch();
exit(0);
}
}
fle1.close();
fle2.close();
}
void unpack()
{
fstream fle2;
fle2.open("r.txt",ios::in);
for(int i=0;i<no;i++)
{
fle2.getline(rec[i].ind,5,'|');
fle2.getline(rec[i].name,20,'|');
fle2.getline(rec[i].usn,20,'|');
fle2.getline(rec[i].branch,20,'\n');
}
fle2.close();
}
void search()
{
char name[20],usn[20],branch[20],ind[5];
unpack();
for(int i=0;i<no;i++)
if(strcmp(st_no,rec[i].ind)==0)
{
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
class buffer;
class student
{
char age[5];
char usn[20];
char name[20];
char branch[10];
char sem[5];
public:
void input();
void output();
friend class buffer;
friend class primaryindexing;
friend class secondaryindexing;
};
class buffer
{
char b[70];
public:
void pack(student &s);
}
}
iobj.writeOffset(offset);
return 0;
}
Output:
1: add record
2: delete record
3:search record
4:exit
1
Enter the USN
1bg09is002
Enter the name
Akshata
Enter the branch
Ise
Enter the semester
Program 7
Write a C++ program to read two lists of names and then match the names in the
two lists using Cosequential Match based on a single loop.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<conio.h>
class coseq
{
char list1[100][20],list2[100][20];
int n1,n2;
public: void load();
Write a C++ program to read k Lists of names and merge them using kway merge
algorithm with k = 8.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
fstream file[8];
char fname[8][8]={"1.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt"};
int no;
class record
{
public:char name[20],usn[20];
}rec[20];
void merge(char* file1,char* file2,char* filename)
{
int k=0;
record rec[20];
fstream f1,f2;
f1.open(file1,ios::in);
f2.open(file2,ios::in);
while(!f1.eof())
{
f1.getline(rec[k].name,20,'|');
f1.getline(rec[k++].usn,20,'\n');
}
Write a C++ program to implement B-Tree for a given set of integers and its
operations insert ( ) and search ( ). Display the tree.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
class node
{
public:
int a[4];
node * next[4];
node * parent;
int size;
node();
};
node :: node()
{
for(int i = 0; i < 4; i++)
next[i] = NULL;
parent = NULL;
size = 0;
}
class btree
{
node * root;
publicc:
Write a C++ program to implement B+ tree for a given set of integers and its
operations insert ( ), and search ( ). Display the tree.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
class node
{
public:
int a[4];
node * next[4];
node * parent;
int size;
node();
};
class linkleaf
{
public:
node * data;
linkleaf *next;
linkleaf();
};
linkleaf :: linkleaf()
{
next = NULL;
}
Write a C++ program to store and retrieve student data from file using hashing.
Use any collision resolution technique.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<iostream.h>
//Record specification
class node
{
public: char name[15],usn[15];
node *link;
};
node *h[29]; //Array of record pointers equal to size of hash keys – 29
void insert()
{
char name[15], usn[15], buffer[50];
fstream out;
out.open("student.txt",ios::app); //opening student.txt in append mode
if(!out)
{
cout<<"\nUnable to open the file in append mode";
getch();
return;
}
cout<<"\nEnter the name = "; cin>>name;
Write a C++ program to reclaim the free space resulting from the deletion of
records using linked lists.
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
class node
{
public:char name[20],usn[20];
node *link;
};
node *first=NULL;
void writefile()
{
fstream out;
node *p;
char buffer[500];
out.open("a1.txt",ios::out);
if(!out)
{
cout<<"cant open the file\n";
getch();
exit(0);
}
p=first;
Menatha 1bg09is048
1: insert
2. delete
3. exit
Enter your choice
1
Name=subash
Usn=1bg09is067