FILE STRUCTURES LABORATORY 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 and 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 and 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. 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. 8. Write a C++ program to read k Lists of names and merge them using kway merge algorithm with k = 8.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
FILE STRUCTURES LABORATORY
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 and 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 and 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.
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.
8. Write a C++ program to read k Lists of names and merge them using kway 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.
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 and student objects with fixed-length records and the fields delimited by “|”.implement pack(),unpack(),modify() and search() methods.
class student{ public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];};
student s2[100];
void writeRecord() //Function to add record to file{ fstream app; student s; app.open("student.txt",ios::app); //Open file in append mode
if(!app) { cout<<"cannot open the file in output mode"; getch(); exit(0); } cout<<"\n Enter the student name = "; cin>>s.name; cout<<"\n Enter the usn = "; cin>>s.usn; cout<<"\n Enter the age = "; cin>>s.age; cout<<"\n Enter the sem = "; cin>>s.sem; cout<<"\n Enter 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++) strcat(s.buffer,"!"); strcat(s.buffer,"\n"); app<<s.buffer; //writing the packed information to buffer app.close();}
if(!in) { cout<<"\nUnable to open the file in input mode"; getch(); exit(0); } 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');
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"; getch(); exit(0); } 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,'\n'); in.getline(extra,45,'\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)
{ cout<<"\n Record with usn "<<usn<<" is not present"; getch(); return; } in.close(); fstream out1; out1.open("student.txt",ios::out); if(!out1) { cout<<"\nUnable to open file in output mode"; getch(); 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); for(int k=0;k<45-count;k++) strcat(buffer,"!");
void main(){ fstream in; fstream out; int ch; out.open("student.txt",ios::out); if(!in) { cout<<"\n\nCannot open the file in output mode"; getch(); exit(0); } out.close();
for(;;) { clrscr(); cout<<"\n 1: write to file\n 2:display the file"
<<"\n 3:modify the file\n 4:search\n 5.exit"; cout<<"\n\n Enter the choice: "; cin>>ch; switch(ch) { case 1: writeRecord();break; case 2: displayFile();break; case 3: modify();break;
case 4: search (); break; case 5: exit(0); default: cout<<"\nInvalid input....";break; } }}
Output :
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = ajayEnter the usn = 1vk07is002Enter the age = 20Enter the sem = 6Enter the branch = ise
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = rahulEnter the usn = 1vk07cs045Enter the age = 20Enter the sem = 6Enter the branch = cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:2Name Usn Age Sem Branchajay 1vk07is002 20 6 ise!!!!!!!!!!!rahul 1vk07cs045 20 6 cse!!!!!!!!!!!
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:4
Enter the record's usn you want to search = 1vk07cs045Record foundrahul 1vk07cs045 20 6 cse!!!!!!!!!!!
1:write to file2:display the file3:modify the file4:search5.exit
3. Write a C++ program to read and write and student objects with variable-length records using any suitable record structure. Implement pack(),unpack(),modify() and search() methods
if(!app) { cout<<"cannot open the file in output mode"; getch(); exit(0); } cout<<"\n Enter the student name = "; cin>>s.name; cout<<"\n Enter the usn = "; cin>>s.usn; cout<<"\n Enter the age = "; cin>>s.age; cout<<"\n Enter the sem = "; cin>>s.sem; cout<<"\n Enter the branch = "; cin>>s.branch;
//packing the information strcpy(s.buffer, s.name); strcat(s.buffer,"|"); strcat(s.buffer, s.usn); strcat(s.buffer,"|");
app<<s.buffer; //writing the packed information to 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"; getch(); exit(0); } 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,'\n');
} } cout<<"\n Record not found"; getch(); 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"; getch(); 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(); getch();}
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"; getch(); exit(0); } 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,'\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;
void main(){ fstream in; fstream out; int ch; out.open("student.txt",ios::out); if(!in) { cout<<"\n\nCannot open the file in output mode"; getch(); exit(0); } out.close();
for(;;) { clrscr(); cout<<"\n 1: write to file\n 2:display the file"
<<"\n 3:modify the file\n 4:search\n 5:exit"; cout<<"\n\n Enter the choice: "; cin>>ch; switch(ch) { case 1: writeRecord();break; case 2: displayFile();break; case 3: modify();break; case 4: search (); break; case 5: exit(0); default: cout<<"\nInvalid input....";break; } }}
Output :
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = ajayEnter the usn = 1vk07is002Enter the age = 20Enter the sem = 6Enter the branch = ise
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:1
Enter the student name = rahulEnter the usn = 1vk07cs045Enter the age = 20Enter the sem = 6Enter the branch = cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:2Name Usn Age Sem Branchajay 1vk07is002 20 6 iserahul 1vk07cs045 20 6 cse
1:write to file2:display the file3:modify the file4:search5.exit
Enter the choice:4
Enter the record's usn you want to search = 1vk07cs045Record foundrahul 1vk07cs045 20 6 cse
1:write to file2:display the file3:modify the file4:search5.exit
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.
class student{ public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];};
void search(){ char usn[15]; int i=0; student s1[100]; cout<<"\nEnter the usn to be searched: "; cin>>usn; fstream in; in.open("student.txt",ios::in); if(!in) { cout<<"\ncannot open the file in output mode"; getch(); exit(0); } i=0;
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++) //Check for the record's existence { if(strcmp(rec[i].usn,usno)==0) flag=i; } if(flag==-1) //Record not found { cout<<"Error !\n"; return; } if(flag==(no-1)) //Delete found record { no--; cout<<"Deleted !\n"; return; } for(i=flag;i<no;i++) { rec[i]=rec[i+1]; } no--; cout<<"\nDeleted !\n"; file2.close(); file1.open("index.txt",ios::out); //Open index and record files file2.open("record.txt",ios::out); //After deletion for(i=0;i<no;i++) //Pack index n record data onto files { 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;}
case 2: cout<<"Enter USN whose record is to be displayed: "; cin>>st_usn; file1.open("index.txt",ios::in); if(!file1) { cout<<"\nError !\n"; exit(0); } flag1=0; for(i=0;i<no;i++) {
file1.getline(rt_usn,20,'|'); //Unpack index file and file1.getline(st_no,4,'\n'); //look for a match in the USN if(strcmp(st_usn,rt_usn)==0)
{ retrieve_details(); //Retrieve details if index found flag1=1;
case 3: cout<<"Enter USN whose record is to be deleted: "; cin>>st_usn; file1.open("index.txt", ios::in); if(!file1) { cout<<"Error! \n"; exit(0); } flag=0; for(i=0;i<no;i++) { file1.getline(rt_usn,20,'|'); //Search index file and
file1.getline(st_no,4,'\n'); //call del if index found if(strcmp(st_usn, rt_usn)==0) {
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.
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.
record recrd[20];int k; k=0;fstream f1,f2;f1.open(file1,ios::in); //open the first filef2.open(file2,ios::in); //open the second filewhile(!f1.eof()) //Unpack and retrieve first file{
int i;int k;k=0;for(i=0;i<8;i+=2) //Merge and sort the 8 original files onto{ //the four files indicated {ll.txt,22.txt....}
merge_file(fname[i],fname[i+1],filename[k++]);}k=4;for(i=0;i<4;i+=2) //Merge and sort the four files onto lll.txt and
222.txt{
merge_file(filename[i],filename[i+1],filename[k++]);}//Merge and sort the two files onto the llll.txt filemerge_file(filename[4],filename[5],filename[6]);return;
}
void main(){
int i;clrscr();cout<<"Enter the no. of records : ";cin>>no;cout<<"\nEnter the details : \n";for(i=0;i<8;i++) //Create 8 files to store the split data
file[i].open(fname[i],ios::out);for(i=0;i<no;i++) //Split and pack data onto the files{
}for(i=0;i<8;i++)file[i].close();kwaymerge(); //Mergefstream result;result.open("lll.txt",ios::in);cout<<"\nSorted Records : \n";char name[20],usn[20];for(i=0;i<no;i++) //Unpack the sorted records and dispL{
}int level;node *leaf = findLeaf(key,level);int flag = 0;for(int i = 0; i < leaf ->size; i++)if(leaf->a[i] == key){
flag = 1;cout<<"The Key "<<key<<" Exists in the B-Tree at the level"<<level<<endl;
}if(!flag)cout<<"The Key Searched for was not found"<<endl;
}
int main(){
btree b;int choice = 1,key;while(choice <=2){
cout<<"1.Insert a Key\n";cout<<"2.Search a key\n";cout<<"3.Exit\n";cout<<”\n\n Enter u’r choice :”;cin>>choice;switch(choice){case 1: cout<<"Enter The Key to be inserted in B-Tree\n";
cin>>key;
b.insert(key);break;
case 2: cout<<"Enter The key to be searched\n";cin>>key;b.search(key);break;
}}return 0;
}
Output :
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree100
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree50-----------50 100-----------1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree75
-------------50 75 100-------------1.Insert a Key2.Search a key3.Exit
Enter u’r choice :1
Enter The Key to be inserted in B-Tree200
---------------50 75 100 200
---------------
1.Insert a Key2.Search a key3.Exit
Enter u’r choice :2
Enter The key to be searched100Key 100 exists in B-tree at level 1
10. Write a C++ program to implement B+ tree for a given set of integers and its operations insert ( ), and search ( ). Display the tree.
flag = 0;if( root == NULL){root = new node;root->a[root->size] = key;root->size++;head = new linkleaf;head -> data = root;return;
}int level;node *leaf = findLeaf(key,level);int i;for(i = 0; i < leaf->size; i++)if(leaf -> a[i] == key){cout<<"The Key to be inserted already exists"<<endl;return;}promote(leaf,key,NULL);cout<<"---------------\n";traverse(root);cout<<"----------------\n";
}void btree :: search(int key){
if(root == NULL){
cout<<"The tree Does not exist"<<endl;return;
}int level;node *leaf = findLeaf(key,level);int flag = 0;for(int i = 0; i < leaf ->size; i++)if(leaf->a[i] == key){
flag = 1;
cout<<"The Key "<<key<<" Exists in the B-Tree at the level"<<level<<endl;
}if(!flag)cout<<"The Key Searched for was not found"<<endl;
}
int main(){
btree b;int choice = 1,key;while(choice <=3){
cout<<"1.Insert a Key\n";cout<<"2.Search a key\n";cout<<"3.Traverse Leaf\n";cout<<"4.Exit\n";cout<<”\n enter u’r choice :”;cin>>choice;switch(choice){case 1: cout<<"Enter The Key to be inserted in B-Tree\n";
cin>>key;b.insert(key);break;
case 2: cout<<"Enter The key to be searched\n";cin>>key;b.search(key);break;
case 3: b.traverseleaf();break;
}}return 0;
}
Output:
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree100
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree50--------50 100--------
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree200
----------50 100 200----------1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 1
Enter The Key to be inserted in B-Tree75---------------50 75 100 200---------------
1.Insert a Key2.Search a key3.Traverse Leaf4.Exit
enter u’r choice : 2
Enter The key to be searched300
The Key Searched for was not found
11. Write a C++ program to store and retrieve student data from file using hashing. Use any collision resolution technique
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; cout<<"\nEnter the usn = "; cin>>usn;
strcpy(buffer,name); //Packing the record onto the file using '|' as a delimiter strcat(buffer,"|"); strcat(buffer,usn); strcat(buffer,"\n"); // \n delimiter for the record out<<buffer; // appending the packed record onto the file out.close();}
//Insert record into the hash tablevoid hash_insert(char name1[], char usn1[], int hash_key){ node *p,*prev,*curr; p = new node; //dynamically allocate space using 'new' strcpy(p->name,name1); strcpy(p->usn,usn1); p->link=NULL; prev=NULL; curr=h[hash_key]; if(curr==NULL) //getting hash pointer location Case: No collision { h[hash_key]=p; return; } while(curr!=NULL) // Case : On collision - Insert at rear end { prev=curr; curr=curr->link; } prev->link=p;}
void retrive(){ fstream in; char name[15],usn[15]; int j,count; node *curr; in.open("student.txt",ios::in); // open record file in input mode if(!in) { cout<<"\n Unable to open the file in input mode"; getch(); exit(0); } while(!in.eof()) { in.getline(name,15,'|'); //unpacking the record in.getline(usn,15,'\n'); count=0; for(j=0; j<strlen(usn); j++) {
if(strcmp(curr->usn,usn)==0) //When the record is found, retrieve{ cout<<"\nRecord found : "<<curr->usn<<" "<<curr->name; getch(); return;}else{ curr=curr->link;}
}while(curr!=NULL); //Search till end of list
if(curr==NULL) //End of list reached with no record found { cout<<"\nRecord not found"; getch(); return;
}}
void main(){ int choice; clrscr(); fstream out; out.open("student.txt",ios::out); if(!out) {
cout<<"\nUnable to open the file in out mode";getch();exit(0);
} for(;;) {
cout<<"\n1:insert\n2: retrive\n3:exit\nEnter the choice - ";cin>>choice;switch(choice){ case 1: insert();
break; case 2: retrive();
break; case 3: exit(0); default: cout<<"\nInvalid option";}
}}
Output :
1:insert2:retrive3:exit
Enter the choice – 1
Enter the name – ajayEnter the usn - 10
1:insert2:retrive3:exit
Enter the choice – 1
Enter the name – rahul Enter the usn - 20
1:insert2:retrive
3:exit
Enter the choice – 1
Enter the name – deepakEnter the usn - 30
1:insert2:retrive3:exit
Enter the choice – 2
Enter the usn = 20
Record found : rahul 20
12. Write a C++ program to reclaim the free space resulting from the deletion of records using linked lists.
void Insert() //Insert the record at the rear end{ char name[20],usn[15]; node *p,*q; cout<<"\n Enter name = "; cin>>name; cout<<"\nEnter usn = "; cin>>usn; p=new node; strcpy(p->name,name); strcpy(p->usn,usn); p->link=NULL; if(first==NULL) {
first=p;writeFile();display(); //display the record on the screenreturn;
} for(q=first; q->link!=NULL; q=q->link) {
; } q->link=p; writeFile(); //writing the record to the file display(); //display the records to the screen.}
void Delete(){ char usn[15]; node *curr,*prev,*del; if(first==NULL) { printf("\nThe list is empty. Deletion is not possible"); return; } cout<<"\nEnter the usn to be deleted = "; cin>>usn;
if(strcmp(first->usn,usn)==0) {
cout<<"\n Record deleted";del = first;delete del;first=first->link;writeFile();return;
} prev=NULL; curr=first; while( ( strcmp(curr->usn,usn) != 0 ) && curr!=NULL) { prev=curr; curr=curr->link; } if(curr == NULL) { cout<<"\nThe student with usn "<<usn<<" is not present"; return; } prev->link = curr->link; writeFile(); display(); //display the records to the screen}
void main(){ int ch; clrscr(); for(;;) {
cout<<"\n 1-Insert_rear \n 2-Delete_id \n 3-Exit \n Enter choice :";
cin>>ch; switch(ch) {
case 1: Insert(); break;
case 2: Delete(); break;
case 3: exit(0); default: cout<<"\n Invalid option";