Skip to main content

File Structure Lab Manual with Algorithms_Program2

Prg2. Write a program to read and write and student objects with fixed-length records and the fields delimited by “|”. 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: Compare usn with the buffer
            if(i==count)
           print as Record not found
           else
           print the name, usn, age, sem and branch
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 fixed-length records and the fields
delimited by “|”
#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);
int x=strlen(buffer);
for(int j=0;j<45-x;j++)
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;
cout<< 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 : Anusha
Enter the usn: 4su16is001
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: 1
Enter the name : Divya
Enter the usn: 4su16is007
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: 2
Name usn age sem branch
Anusha 4su16is001 21 6 ISE
Divya 4su16is007 20 6 ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 3
Enter the USN: 4su16is001
Record found
Name usn age sem branch
Anusha 4su16is001 21 6 ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 4
Enter the USN: 4su16is007
Record found
Enter new values
Enter name: Arpita
Enter USN: 4su16is003
Enter age: 22
Enter sem: 5
Enter branch: CSE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 2
Name usn age sem branch
Arpita 4su16is003 22 5 CSE
Divya 4su16is007 20 6 ISE
1. Write 2 .Display 3. Search 4. Modify 5. Exit
Enter your choice: 5

Viva Voce:

1. Define Field.
Each element of a record is known as field.
2. Define record.
Set of fields constitutes a file is called record.
3. What do you mean by delimiter? List any two delimiters.
Special mark used to differentiate between two fields. Ex: comma(,), pipe(|)
4. Differentiate between field and record.
A field is smaller unit of a record and record is set of fields constitutes a file.
5. What do you mean by fixed length record?
A fixed-length record is one in which every field has a fixed length.
6. Differentiate fixed length record with variable length record?
A fixed-length record is one in which every field has a fixed length. A variable-length
record has at least one variable-length field.
7. 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.
8. What are the different ways of identifying record structures?
Fixed Length Records, Delimited Variable Length Records, Length Prefixed Variable
Length Records.
9. 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.
10. How do we delete fixed length records?
Using file header.

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