FILE ORGANIZATION - Single Level
FILE ORGANIZATION - Single Level
Algorithm:
1. Start the program and include all the necessary header files.
2. Create structures for the file and file directory and declare the necessary variables.
3. Create InsertFile() to create files in the directory.
4. Create DisplayFiles() to print the file names that are in the directory.
5. Create DeleteFile() to delete a file from the directory.
If the file is present , it will be deleted else no deletion occurs.
6. Create SearchFile() to search the file required from the directory.
If the file if present in the directory, the file position will be displayed
Else “the file is not found” will be displayed .
7. Enter the directory name dir.dirName
8. Choose a options to perform operation – insert, display, delete, search, exit.
9. Print the result
10. Stop
Program coding:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct file{
char fileName[15][20];
char dirName[10];
int fno;
};
struct file dir;
int i,n;
void InsertFile(){
printf("\n Enter the File name ");
scanf("%s",dir.fileName[dir.fno]);
dir.fno++;
}
void DisplayFiles(){
printf("\n\n\n\n");
printf("+------------------------+");
printf("\n Directorytfiles | \n");
printf("+------------------------+");
printf("\n %s",dir.dirName);
for(i=0;i<dir.fno;i++){
printf("\n \t\t%s",dir.fileName[i]);
}
printf("\n+------------------------+");
printf("\n\n\n\n");
}
void DeleteFile(){
char name[20];
printf("\n Enter the file to be deleted : ");
scanf("%s",name);
for(i=0;i<dir.fno;i++){
if(strcmp(dir.fileName[i],name)==0){
printf("%s is deleted \t",dir.fileName[i]);
strcpy(dir.fileName[i],dir.fileName[dir.fno-1]);
dir.fno--;
}
}
}
void SearchFile(){
char name[20];
int found=-1;
printf("\n Enter the file to be searched :");
scanf("%s",name);
for(i=0;i<dir.fno;i++){
if(strcmp(dir.fileName[i],name)==0){
printf("\n The File is found at position %d",i+1);
found=1;
break;
}
}
if(found==-1)
printf("\n the file is not found ");
}
int main(){
int op;
dir.fno=0;
printf("\n Enter the directory name : ");
scanf("%s",dir.dirName);
while(1){
printf("\n choose the option \n1:Insert a file \n2:Display Files
\n3:Delete File \n4:Search File \n5:Exit \n>>");
scanf("%d",&op);
switch(op){
case 1:InsertFile();
break;
case 2:DisplayFiles();
break;
case 3:DeleteFile();
break;
case 4:SearchFile();
break;
case 5:exit(0);
}
}
return 0;
}
Output:
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>1
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>1
2:Display Files
3:Delete File
4:Search File
5:Exit
>>1
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>2
+------------------------+
Directory files |
+------------------------+
MyFolder
hello.txt
new.c
last.txt
+------------------------+
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>4
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>3
new.c is deleted
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>2
+------------------------+
Directory files |
+------------------------+
MyFolder
hello.txt
last.txt
+------------------------+
choose the option
1:Insert a file
2:Display Files
3:Delete File
4:Search File
5:Exit
>>5
Result:
Thus the C program to implement single level directory technique was written, executed and
the output was verified