Single Level Directory Program In C

There are many types of structures of directory in the Operating System. The simplest one of them is Single Level Directory structure. In this type of structure, there is one user and there is a single root directory. Inside the root directory, there is a main entry directory responsible for a single file. All the files of the system are contained inside this main root directory. These main directories house all the files in the system. There can be no other sub-directories inside the main directories. Implementation of such a structure is very simple, and searching is also very easy. But this also has a big disadvantage which is that there cannot be two files of the same name. Even if the size of the files remains small, the size of the directory will be huge because a system has thousands of such files and all of these will be inside a single root directory.

Single Level Directory in OS

Single Level Directory Program In C++

/* ******************************
****************************** */
#include <iostream>
#include <string>

using namespace std;

string root, dirNames[10], fNames[100];
int flag = 0, fCount[10] = {0};

int searchDir(string tempDir){
    for (int i = 0; i < flag; i++){
        if (dirNames[i] == tempDir)
            return i;
    return -1;

int searchFile(string tempFile, int i, int x){
    for (int j = i; j < x; j++){
        if (dirNames[j] == tempFile)
            return j;
    return -1;

void addDir(){
    string tempDir;
    cout << "Enter the name of the directory to be created: ";
    cin >> tempDir;
    int x = searchDir(tempDir);
    if (x != -1)
        cout << "\nDirectory already exists!\n";
        dirNames[flag++] = tempDir;

void addFile(){
    int cv = 1;
    string temp;
    if (flag == 0){
        cout << "\nPlease create a directory first!\n";
    cout << "Enter the name of the directory inside which the file is to be created: ";
    cin >> temp;
    int x = searchDir(temp);
    int i = x;
    if (x == -1){
        cout << "\nDirectory does not exist!" << endl;
    x = x * 10;
    x = x + fCount[i];
    while (cv){
        string tempFile;
        cout << "Enter the name of the file to be created inside '" << temp << "': ";
        cin >> tempFile;
        int y = searchFile(tempFile, i, x);
        if (y != -1)
            cout << "\nFile already exists!\n";
            fNames[x] = tempFile;
        int tempch;
        cout << "\nPress 1 to create more files, Press 0 for Main Menu: ";
        cin >> tempch;
        cv = tempch;

void displayDirTree(){
    cout << "\n*********************************************\n";
    cout << "\t||USERNAME: " << root << "||\t\n";
    cout << "*********************************************\n";
    cout << "\tDIRECTORY\tSIZE\tFILES\n";
    cout << "*********************************************\n";
    for (int i = 0; i < flag; i++){
        cout << "\n\t" << dirNames[i] << "\t\t" << fCount[i] << "\t";
        if (fCount[i] == 0)
            cout << "NONE\n";
        else {
            for (int j = i*10; j < fCount[i]+(i*10); j++){
                cout << fNames[j] << "\n\t\t\t\t";
    cout << "\n*********************************************\n";

int main(){
    cout << "Enter the User Name: ";
    getline(cin, root);
    int ch, x = 1;
    while (x){
        cout << "\nPress 1 to create a directory inside root\n";
        cout << "Press 2 to create a file inside a directory\n";
        cout << "Press 3 to display the directory tree\n";
        cout << "Press 0 to exit\n";
        cin >> ch;
            case 0: x = 0;
            case 1: addDir();
            case 2: addFile();
            case 3: displayDirTree();
    return 0;

Output of Single Level Directory Program In C++

Output of Single Level Directory Program In C++

In the above program, the main () function is used to take the user name from the user. It is also responsible for taking the choice from the user repeatedly till the user enters 0 for exit. Pressing 1 calls the addDir () method which is used to add a directory inside the root folder. Pressing 2 calls the addFile () method, which will take the name of the directory inside which a file has to be created and then the name of the file and then it will create a file inside the respective directory. Pressing 3 will call the displayDirTree () method which will display the directory tree created by the user.

Leave a Comment