Single-level directory Program in C

In the operating system, directories can be organized in a variety of ways. Single Level Directory Structure is the most straightforward of them all. The root directory and the user are the only components of this kind of organization. A primary entry directory that places just one file is located inside the root directory. This system's primary root directory places all of the system's files. All system files are kept in these primary folders. Inside the primary directories, there cannot be any further subdirectories. Such a framework is quite easy to implement, and it makes searching very simple. The fact that there can never be two files with the same name, however, is a significant drawback of this. A system has hundreds of these files, all of which will be contained inside a single root directory. Thus, even if the number of files is tiny, the size of the directory will be enormous.

The functionalities of this program allow for the insertion of files, deletion of files, searching for files, and display of existing files.

Example:

Let's take a program to demonstrate the single-level directory in C:

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:

  1. 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.
  2. 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.
  3. 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.
  4. Define several functions:
    • Insertin_File(): The user can add a file name to the directory using the Insertin_File() 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.
    • Deleting_File(): The Deleting_File() 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.
    • Searching_File(): Using the name of the file, the user can search for it in the directory using the Searching_File() If a file is found, its location is displayed.
  5. During the main() function:
    1. Set the dir.fno (number of files) initial value to 0.
    2. Request the directory name from the user and store it in dir.dir_Name.
    3. Display a menu and repeatedly prompt the user to select an operation using a while loop.
    4. 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 code offers a straightforward command-line interface to control files inside a directory. It is a basic example for educational purposes because it lacks error handling and does not maintain data across program runs.