Example:
Let's take a program to demonstrate the single-level directory in C:
Example
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct file{
char file_Name[15][20];
char dir_Name[10];
int fno;
};
struct file dir;
int i,n;
void Inserting_File()
{
printf("Enter the File name: ");
scanf("%s",dir.file_Name[dir.fno]);
dir.fno++;
}
void Displaying_Files()
{
printf("\n\n\n\n");
printf("+------------------------+");
printf("\n Directory\tfiles | \n");
printf("+------------------------+");
printf("\n %s",dir.dir_Name);
for(i=0;i<dir.fno;i++)
{
printf("\n \t\t%s",dir.file_Name[i]);
}
printf("\n+------------------------+");
printf("\n\n\n\n");
}
void Deleting_File()
{
char name[20];
printf("\n Enter the file name to be deleted : ");
scanf("%s",name);
for(i=0;i<dir.fno;i++){
if(strcmp(dir.file_Name[i],name)==0){
printf("%s is deleted. \t",dir.file_Name[i]);
strcpy(dir.file_Name[i],dir.file_Name[dir.fno-1]);
dir.fno--;
}
}
}
void Searching_File()
{
char name[20];
int found=-1;
printf("\n Enter the file name to be searched :");
scanf("%s",name);
for(i=0;i<dir.fno;i++)
{
if(strcmp(dir.file_Name[i],name)==0)
{
printf("\n The particular 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("Enter the directory name: ");
scanf("%s",dir.dir_Name);
while(1)
{
printf("\n choose the option \n1:Inserting the file\n2:Displaying the all Files\n3:Deleting the File\n4:Searching the File\n5:Exit\n>>");
scanf("%d",&op);
switch(op)
{
case 1:Inserting_File();
break;
case 2:Displaying_Files();
break;
case 3:Deleting_File();
break;
case 4:Searching_File();
break;
case 5:exit(0);
}
}
return 0;
}
Output:
Output
Enter the directory name: myfolder
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>1
Enter the File name: hii.txt
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>1
Enter the File name: newone.c
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>1
Enter the File name: lastone.txt
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>2
+------------------------+
Directory files |
+------------------------+
myfolder
hii.txt
newone.c
lastone.txt
+------------------------+
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>4
Enter the file name to be searched:newone.c
The particular File is found at position 2
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>3
Enter the file name to be deleted: lastone.txt
lastone.txt is deleted.
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>2
+------------------------+
Directory files |
+------------------------+
myfolder
hii.txt
newone.c
+------------------------+
choose the option
1:Inserting the file
2:Displaying All Files
3:Deleting the File
4:Searching the File
5:Exit
>>5
Explanation:
- Include the header files that are required: Use h header file for input and output operations . For functions involving manipulating strings, h . Use h for the exit function .
- Create a 'struct' called 'file': fileName[15] in char[20]: A 2D array for storing the directory's file names. Each filename that it stores can be up to 15 characters long. char dirName[10] : A character array used to store the directory's name (up to 10 characters). int fno : An integer used to record the number of files in the directory.
- Declare the variables dir, i, and n as global. The integer variables i and n are utilized in numerous functions, while dir is an instance of the file struct.
- Define several functions: InsertinFile: The user can add a file name to the directory using the InsertinFile method . It asks for input from the user and adds the filename to the directory structure. DisplayFiles: DisplayFiles formats the names of the files in the directory and displays them with the directory name. DeletingFile: The DeletingFile function enables users to remove specific files from directories by providing their names. By name, it looks for the file and deletes it if it is discovered. SearchingFile: Using the name of the file, the user can search for it in the directory using the SearchingFile If a file is found, its location is displayed.
- During the main function: Set the dir.fno (number of files) initial value to 0. Request the directory name from the user and store it in dir.dir_Name . Display a menu and repeatedly prompt the user to select an operation using a while loop. Read the user's selection (op) inside the loop, and then use a switch statement to execute the appropriate function by the selection.
- Use h header file for input and output operations .
- For functions involving manipulating strings, h .
- Use h for the exit function .
- fileName[15] in char[20]: A 2D array for storing the directory's file names. Each filename that it stores can be up to 15 characters long.
- char dirName[10] : A character array used to store the directory's name (up to 10 characters).
- int fno : An integer used to record the number of files in the directory.
- InsertinFile: The user can add a file name to the directory using the InsertinFile method . It asks for input from the user and adds the filename to the directory structure.
- DisplayFiles: DisplayFiles formats the names of the files in the directory and displays them with the directory name.
- DeletingFile: The DeletingFile function enables users to remove specific files from directories by providing their names. By name, it looks for the file and deletes it if it is discovered.
- SearchingFile: Using the name of the file, the user can search for it in the directory using the SearchingFile If a file is found, its location is displayed.
- Set the dir.fno (number of files) initial value to 0.
- Request the directory name from the user and store it in dir.dir_Name .
- Display a menu and repeatedly prompt the user to select an operation using a while loop.
- Read the user's selection (op) inside the loop, and then use a switch statement to execute the appropriate function by the selection.
Case 1: Add a file to the directory.
Case 2: Show the directory's files.
Case 3: Delete a file from the directory.
Case 4: Check the directory for a file.
Case 5: Use exit(0) to end the program.
This script provides a simple command-line interface for managing files within a specified directory. It serves as a fundamental illustration for educational use, as it does not include error management and does not retain data between program executions.