Skip to main content

File Structure Lab Manual with Algorithms_Program3

Program 3. Write a program to read and write and student objects with variable-length records using any suitable record structure. Implement pack(), unpack(), modify() and search() methods.

Algorithm:
Step 1: Start
Step 2: Enter the choice
           1.Write 2. Display 3. Search 4. Modify 5.Exit
           case 1: write() //Read the name, usn, age, sem, branch
           case 2: unpack()
                       display()
           case 3: unpack()
                       search()
           case 4: unpack()
                      search()
                       modify()

Step 3: Stop


Algorithm: write()
Step 1: Start
Step 2: Enter the name, usn, age, sem, branch
Step 3: call pack(t) function
Step 4: Stop


Algorithm: pack(t)
Step 1: Start
Step 2: Open a file hello.txt
Step 3: copy the strings- name, usn, age, branch and concatenate in buffer
Step 4: Now read length of buffer -strlen(buffer)
Step 5: close a file
Step 6: Stop

Algorithm: unpack()
Step 1: Start
Step 2: open a file hello.txt
Step 3: Read each line from the buffer -getline(buffer,100)
Step 5: close a file
Step 6: Stop

Algorithm: display()
Step 1: Start
Step 2: if(count==0), print No records
           else read name, usn, age, sem, branch

Step 3: Print name, usn, age, sem, branch
Step 4: Stop


Algorithm: search()
Step 1: Start
Step 2: Enter the usn
Step 3: if(!strcmp(s[i].usn,temp))
print the name, usn, age, sem and branch
else
print as Record not found
Step 4: Stop


Algorithm: modify()
Step 1: Start
Step 2: Enter new value to modify
Step 3: Update the name, usn, age, sem, branch
Step 4: call pack(s[j]);
Step 5: Stop


Program:
// Program to read and write and student objects with variable-length records using any suitable
record structure
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<fstream.h>
class student
{
public:char name[20], usn[10], age[5], sem[5],branch[5];
};
student s[100],t;
char buffer[45],temp[20];
int count=0,i;
fstream fp;

void pack(student p)
{
fp.open("hello.txt",ios::app);
strcpy(buffer,p.name);
strcat(buffer,"|");
strcat(buffer,p.usn);
strcat(buffer,"|");
strcat(buffer,p.age);
strcat(buffer,"|");
strcat(buffer,p.sem);
strcat(buffer,"|");
strcat(buffer,p.branch);
strcat(buffer,"|");
fp<<buffer<<endl;
fp.close();
}
void write()
{
cout<<"Enter the name\n";
cin>>t.name;
cout<<"Enter the usn\n";
cin>>t.usn;
cout<<"Enter the age\n";
cin>>t.age;
cout<<"Enter the sem\n";
cin>>t.sem;
cout<<"Enter the branch\n";
cin>>t.branch;
pack(t);
}

void unpack()
{
fp.open("hello.txt",ios::in);
for(i=0;i<count;i++)
{
fp.getline(buffer,100);
sscanf(buffer,"%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|",s[i].name,s[i].usn,
s[i].age,s[i].sem,s[i].branch);
}
fp.close();
}
void display()
{
if(count==0)
{
cout<<"\nNo records\n";
return;
}
cout<<"\n name\t usn\t age\t sem\t branch\n";
for(i=0;i<count;i++)
cout<<s[i].name<<"\t"<<s[i].usn<<"\t"<<s[i].age<<"\t"<<s[i].sem<<
"\t"<<s[i].branch<<endl;
}
void search()
{
cout<<"Enter the usn\n";
cin>>temp;
for(i=0;i<count;i++)
if(!strcmp(s[i].usn,temp))
{

cout<<"Record found\n"<<s[i].name<<"\t"<<
s[i].usn<<"\t"<<s[i].age<<"\t"<<s[i].sem<<"\t"<<s[i].branch<<endl;
break;
}
if(i==count)
cout<<"Record not found";
}

void modify()
{
if(i==count)
return;
cout<<"Enter new values\n Enter name :";
cin>>s[i].name;
cout<<"Enter usn :";
cin>>s[i].usn;
cout<<"Enter age :";
cin>>s[i].age;
cout<<"Enter sem :";
cin>>s[i].sem;
cout<<"Enter branch :";
cin>>s[i].branch;
fp.close();
remove("hello.txt");
fp.open("hello.txt",ios::out);
fp.close();
for(int j=0;j<count;j++)
pack(s[j]);
}
void main()

{
int c;
clrscr();
fp.open("hello.txt",ios::out);
fp.close();
while(1)
{
cout<<"\n1.Write\n 2.Display\n 3.Search\n 4.Modify\n 5.Exit\n Enter your

choice\n";

cin>>c;
switch(c)
{
case 1:count++;write();break;
case 2:unpack();display();break;
case 3:unpack();search();break;
case 4:unpack();search();modify();break;
default:exit(0);
}
}
}

Output:
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 1
Enter the name : Arun
Enter the usn: 4su16is002
Enter the age: 20
Enter the Sem: 6
Enter the branch : ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 1
Enter the name: Kiarn

Enter the usn: 4su16is013
Enter the age: 21
Enter the Sem: 6
Enter the branch: ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 2
Name usn age sem branch
Arun 4su16is002 20 6 ISE
Kiran 4su16is013 21 6 ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 2
Enter the USN: 4su16is013
Record found
Name usn age sem branch
Kiran 4su16is013 21 6 ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 4
Enter the USN: 4su16is007
Record found
Name usn age sem branch
Kiran 4su16is013 21 6 ISE
Enter new values
Enter name: Harsha
Enter USN: 4su16is009
Enter age: 21
Enter sem: 6
Enter branch: CSE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 2
Name usn age sem branch
Arun 4su16is002 20 6 ISE
Harsha 4su16is009 21 6 CSE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 5

Viva Voce:
1. What do you mean by a record?
Record is the set of fields that constitutes a file.
2. How do you represent a record in a file?
Using fields.
3. Explain need for variable length records.
Conserves disk space by using just the space needed to hold variable length data.
4. Explain different ways of representing variable length records.
Storage of multiple record types in a file, record types that allow variable lengths for one
or more fields, record types that allow repeating fields
5. Differentiate between fixed and variable length records.
Fixed-length record is one in which every field has a fixed length. A variable-length
record has at least one variable-length field.
6. What are the different ways of identifying fields?
Fixed Length Fields, Delimited Variable Length Fields, Length Prefixed Variable Length
Fields, Representing Record or Field Length, Tagged Fields.
7. What are the different ways of identifying record structures.?
Fixed Length Records, Delimited Variable Length Records, Length Prefixed Variable
Length Records.
8. What do you mean by pack and unpack?
The pack and unpack are the commands which are primarily used for creating or reading
binary structures.
9. How do we delete variable length records?
Using Avail List.
10. Define sequential access.
Access to a file that requires the user to read through the file from the beginning in the
order in which it is stored.

Comments

Popular posts from this blog

Write a program for error detecting code using CRC-CCITT (16- bits).

import java.io.*; import java.util.*; public class CRC   {   public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter the no of bits : "); int   n=s.nextInt(); int data[ ]=new int[n]; System.out.println("Enter the data bits : "); for(int i=0;i<n;i++) data[i]=s.nextInt(); System.out.println("Enter the no of divisor bits : "); int m=s.nextInt(); int divisor[ ]=new int[m]; System.out.println("Enter divisor bits : "); for(int j=0;j<m;j++) divisor[j]=s.nextInt(); int len=n+m-1; int div[ ]=new int[len]; int rem[ ]=new int[len]; int crc[ ]=new int[len]; int src[ ]=new int[len]; for(int i=0;i<data.length;i++) div[i]=data[i]; System.out.println("Dividend after appending zero"); for(int i=0;i<div.length;i++) System.out.print(div[i]); System.out.println(); for(int j=0;j<div.length;j++) rem[j]=div[j]; rem=divid

Write a program for congestion control using leaky bucket algorithm

import java.util.*; public class LBA {        public static void main(String[] args ) {              // TODO Auto-generated method stub              int op ;              Scanner s = new Scanner(System. in );              int bktcap =0;              int remain =0;              int pkt []= new int [10];              System. out .println( "Enter Bucket size" );              bktcap = s .nextInt();              System. out .println( "Enter value n" );              int n = s .nextInt();              System. out .println( "Enter datarate" );              int dr = s .nextInt();              System. out .println( "Enter input values" );              for ( int i =0; i < n ; i ++){              int val = s .nextInt();              pkt [ i ]= val ;              }              for ( int i =0; i <= n ; i ++){                     int total = pkt [ i ]+ remain ;                     if ( t