0% found this document useful (0 votes)
20 views

Program-8 - File Organization Techniques

Uploaded by

sreelakshmikn33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Program-8 - File Organization Techniques

Uploaded by

sreelakshmikn33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Computer Science Engineering – ( Data Science)

Program 8: Simulate following File Organization Techniques a) Single level directory b) Two level directory

 A directory is a container that is used to contain folders and files. It organizes files and folders in a
hierarchical manner.
 Just like we use folders to keep your papers and documents in order, the operating system uses
directories to keep track of files and where they are stored.
 Different structures of directories can be used to organize these files, making it easier to find and
manage them.
1. Single-Level Directory
 The single-level directory is the simplest directory structure. In it, all files are contained in the
same directory which makes it easy to support and understand.

2. Two-Level Directory
 In the two-level directory structure, each user has their own user files directory (UFD).
 The UFDs have similar structures, but each lists only the files of a single user.
 System’s master file directory (MFD) is searched whenever a new user id is created.

Operating Systems Lab[BCS303] 1


Department of Computer Science Engineering – ( Data Science)

Program: Single Level Directory

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch; char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory: ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \t 4. Display Files\t5. Exit\n
Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{

if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)

Operating Systems Lab[BCS303] 2


Department of Computer Science Engineering – ( Data Science)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)

printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

Sample Output:

Enter name of directory: CSE


1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit

Enter your choice : 1


Enter the name of the file -- file1

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 1
Enter the name of the file -- file2

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 1
Enter the name of the file -- file3

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 4
The Files are -- file1 file2 file3

Operating Systems Lab[BCS303] 3


Department of Computer Science Engineering – ( Data Science)
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit
Enter your choice : 3

Enter the name of the file -- file2


File file2 is found

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 2

Enter the name of the file -- file2


File file2 is deleted

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 3

Enter the name of the file -- file2


File file2 not found

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 4

The Files are -- file1 file3

1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit


Enter your choice : 5

Operating Systems Lab[BCS303] 4


Department of Computer Science Engineering – ( Data Science)

Program: Two-Level Directory


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf ("\n4. Search File\t\t5. Display\t6. Exit\t “);
printf("\nEnter your choice—");
scanf("%d",&ch);
switch(ch)
{
case 1: printf ("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf ("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf ("File created");
break;
}
if(i==dcnt)
printf ("Directory %s not found",d);

Operating Systems Lab[BCS303] 5


Department of Computer Science Engineering – ( Data Science)
break;
case 3: printf ("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf ("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf ("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
}
}
printf ("Directory %s not found",d);
jmp : break;
case 4: printf ("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf ("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf ("File %s is found ",f);
goto jmp1;
}
}
printf ("File %s not found",f);
goto jmp1;
}

Operating Systems Lab[BCS303] 6


Department of Computer Science Engineering – ( Data Science)
}
printf ("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf ("\nNo Directory's ");
else
{
printf ("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf ("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf ("\t%s",dir[i].fname[k]);
}
}
break;
default: exit(0);
}
}
}

Sample output

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice---1

Enter name of directory -- cse


Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---1

Enter name of directory -- ds


Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---2

Enter name of the directory -- cse


Enter name of the file -- c1
File created

Operating Systems Lab[BCS303] 7


Department of Computer Science Engineering – ( Data Science)
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---2

Enter name of the directory -- cse


Enter name of the file -- c2
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---2

Enter name of the directory -- ds


Enter name of the file -- d1
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---5

Directory Files
cse c1 c2
ds d1
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---4

Enter name of the directory -- cse


Enter the name of the file -- c1
File c1 is found
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---3

Enter name of the directory -- ds


Enter name of the file -- d1
File d1 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice---6

Operating Systems Lab[BCS303] 8

You might also like