Skip to main content

Write a program to simulate Sequential (Contiguous) file allocation method. Assume disk with n number of blocks. Give value of n as input. Write menu driver program with menu options as mentioned above and implement each option.

 



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int bv[50],i,st,j,len,c,k;
char name[10][30];
int start[10],length[10],num=0;

void displayBitVector()
{
     printf("\nBit Vector contents : \n");
     for (i=0;i<50;i++)
          printf("%d ",bv[i]);
}

void createFile()
{
     char temp[30];
     printf("Enter the name of file : ");
     scanf("%s",&temp);
     for (int i=0;i<num;i++)
          if (strcmp(name[i],temp)==0)
          {
              printf("\nFilename already exists");
              return;
          }
     strcpy(name[num],temp);
     printf("Enter the start block of the file : ");
     scanf("%d",&start[num]);
     printf("Enter the length of the file : ");
     scanf("%d",&length[num]);
     
     for (j=start[num];j<(start[num]+length[num]);j++)
          if (bv[j]==1)
          {
              printf("File cannot be allocated.... block already allocated");
              strcpy(name[j],"-");
              start[j]=0;
              length[j]=0;
              return;
          }
     for (j=start[num];j<(start[num]+length[num]);j++)
     {
          bv[j]=1;
          printf("\n%d->%d",j,bv[j]);
     }
     num++;
}

void showDirectory()
{
      printf("Directory contents\n");
      printf("%s%40s%40s\n","File Name","Start Block","Length");
      int i;
      for (i=0;i<num;i++)
      {
           if (strcmp(name[i],"-")!=0)
           {
               printf("%s%40d%40d\n",name[i],start[i],length[i]);
           }
      }
}

void deleteFile()
{
     char temp[10];
     printf("\nEnter the filename : ");
     scanf("%s",&temp);
     for (int i=0;i<num;i++)
     {
          if (strcmp(name[i],temp)==0)
          {
             
                   for (j=start[i];j<(start[i]+length[i]);j++)
                   {
                        bv[j]=0;
                        printf("\n%d->%d",j,bv[j]);
                   }
                   printf("\nFile successfully deleted...");
                   strcpy(name[i],"-");
                   start[i]=0;
                   length[i]=0;
                   return;
             
          }
     }
}

int main()
{
    int ch=0;
    for (i=0;i<50;i++)
         bv[i]=0;
         
    do
    {
        printf("\n*******Menu*******\n");
        printf("\n1.Show bit Vector");
        printf("\n2.Create New File");
        printf("\n3.Show Directory");
        printf("\n4.Delete File");
        printf("\n5.Exit\n");
        printf("\nEnter Your Choice : \n");
        scanf("%d",&ch);
       
        switch (ch)
        {
                case 1: displayBitVector();
                        break;
                case 2: createFile();
                        break;
                case 3: showDirectory();
                        break;
                case 4: deleteFile();
                        break;
        }
    }while(ch!=5);
}

/**
***********OUTPUT***********
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
1

Bit Vector contents :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
2
Enter the name of file : harsh
Enter the start block of the file : 2
Enter the length of the file : 13

2->1
3->1
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
14->1
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
3
Directory contents
File Name                             Start Block                                  Length
harsh                                       2                                      13

*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
2
Enter the name of file : shivam
Enter the start block of the file : 16
Enter the length of the file : 5

16->1
17->1
18->1
19->1
20->1
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
1

Bit Vector contents :
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
4
harsh

2->0
3->0
4->0
5->0
6->0
7->0
8->0
9->0
10->0
11->0
12->0
13->0
14->0
File successfully deleted...
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
1

Bit Vector contents :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
4
shivam

16->0
17->0
18->0
19->0
20->0
File successfully deleted...
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
1

Bit Vector contents :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*******Menu*******

1.Show bit Vector
2.Create New File
3.Show Directory
4.Delete File
5.Exit
Enter Your Choice :
5

**/

Comments

Popular posts from this blog

Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file: i. Search for a specific book by name. ii. Display all book and total cost

  import java . io .*; import java . util .*; class Book {       String name , id ;       int qty ;       double price , total ;       Book ( String i , String n , String p , String q )      {               name = n ;               id = i ;               qty = Integer . parseInt ( q );               price = Double . parseDouble ( p );               total = qty * price ;      }       public String toString ()      {               System . out . println ( "name\t id\t qty\t price\t total" );               String s = name + "\t" + id + "\t" + qty + "\t" + price + "\t" + total ;           ...

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid. Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021

  import java . io .*; import java . util .*; class InvalidDateException extends Exception {       InvalidDateException ()       {               System . out . println ( "Invalid Date" );       } } class MyDate {       int day , mon , yr ;       void accept ( int d , int m , int y )       {             day = d ;             mon = m ;             yr = y ;       }       void display ()       {             System . out . println ( "Date is valid : " + day + "/" + mon + "/" + yr );       } } class Date {       public static void main ( String args []) throws Exception       {             Scanner sc = new Sca...

Write a Java program to design a screen using Awt that will take a user name and password. If the user name and password are not same, raise an Exception with appropriate message. User can have 3 login chances only. Use clear button to clear the TextFields.

  import java . awt .*; import java . awt . event .*; import javax . swing .*; class InvalidPasswordException extends Exception {       InvalidPasswordException ()       {             System . out . println ( "Username and password is not same" );       } } public class Password extends Frame implements ActionListener {       Label uname , upass ;       TextField nametext ;       TextField passtext , msg ;       Button login , Clear ;       Panel p ;       int attempt = 0 ;       char c = '*' ;             public void login ()       {             p = new Panel ();             uname = new Label ( "Username : " , Label . CENTER );             upass = n...