Google Advertisement

Simple File System Program in C for Create, Read, and Write Operations

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

Learn how to build a simple file system in C that supports file creation, reading, and writing. Full source code with step-by-step explanations in easy words.

Published on 17 May 2025
By Vandu

In this tutorial, we’ll implement a basic file system using the C programming language that allows you to:

This type of small project helps you understand how file handling works in Operating Systems, especially how low-level file management can be simulated in user space.


πŸ” What is a File System?

A file system is a way to organize and store files on a storage device like a hard drive or SSD. In Operating Systems, it handles file creation, reading, writing, permissions, and organization. For this project, we’ll simulate a tiny file system where we manually perform basic operations.


🎯 Program Goals

Our simple file system will support:

We’ll use file handling functions in C (fopen, fprintf, fscanf, etc.) to perform these operations.


πŸ§‘‍πŸ’» C Code: Basic File System Program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function declarations
void createFile(const char *filename);
void writeFile(const char *filename);
void readFile(const char *filename);

int main() {
    int choice;
    char filename[100];

    while (1) {
        printf("\n=== Simple File System Menu ===\n");
        printf("1. Create File\n");
        printf("2. Write to File\n");
        printf("3. Read from File\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // to consume newline character

        switch (choice) {
            case 1:
                printf("Enter filename to create: ");
                fgets(filename, sizeof(filename), stdin);
                filename[strcspn(filename, "\n")] = '\0';  // remove newline
                createFile(filename);
                break;

            case 2:
                printf("Enter filename to write to: ");
                fgets(filename, sizeof(filename), stdin);
                filename[strcspn(filename, "\n")] = '\0';
                writeFile(filename);
                break;

            case 3:
                printf("Enter filename to read from: ");
                fgets(filename, sizeof(filename), stdin);
                filename[strcspn(filename, "\n")] = '\0';
                readFile(filename);
                break;

            case 4:
                printf("Exiting program...\n");
                exit(0);

            default:
                printf("Invalid choice! Please try again.\n");
        }
    }

    return 0;
}

// Function to create an empty file
void createFile(const char *filename) {
    FILE *fp = fopen(filename, "w");  // open in write mode
    if (fp == NULL) {
        printf("Error creating file!\n");
        return;
    }
    printf("File '%s' created successfully.\n", filename);
    fclose(fp);
}

// Function to write content to a file
void writeFile(const char *filename) {
    FILE *fp = fopen(filename, "a");  // append mode
    if (fp == NULL) {
        printf("File not found!\n");
        return;
    }

    char data[500];
    printf("Enter text to write (end with ENTER):\n");
    fgets(data, sizeof(data), stdin);
    fprintf(fp, "%s", data);

    printf("Data written to '%s' successfully.\n", filename);
    fclose(fp);
}

// Function to read content from a file
void readFile(const char *filename) {
    FILE *fp = fopen(filename, "r");  // open in read mode
    if (fp == NULL) {
        printf("File not found!\n");
        return;
    }

    char ch;
    printf("\n--- File Contents of '%s' ---\n", filename);
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
    }
    printf("\n--- End of File ---\n");
    fclose(fp);
}
 

🧠 Step-by-Step Explanation

πŸ”Ή main() Function


πŸ”Ή createFile() Function

FILE *fp = fopen(filename, "w");
 

πŸ”Ή writeFile() Function

FILE *fp = fopen(filename, "a");
 

πŸ”Ή readFile() Function

char ch;
while ((ch = fgetc(fp)) != EOF) {
    putchar(ch);
}
 

βš™οΈ How to Compile and Run

Using GCC compiler:

gcc file_system.c -o file_system
./file_system
 

Make sure your terminal/IDE is in the same folder where your file_system.c file is saved.


βœ… Output Example

=== Simple File System Menu ===
1. Create File
2. Write to File
3. Read from File
4. Exit
Enter your choice: 1
Enter filename to create: myfile.txt
File 'myfile.txt' created successfully.

Enter your choice: 2
Enter filename to write to: myfile.txt
Enter text to write (end with ENTER):
Hello from my mini file system!
Data written to 'myfile.txt' successfully.

Enter your choice: 3
Enter filename to read from: myfile.txt

--- File Contents of 'myfile.txt' ---
Hello from my mini file system!
--- End of File ---
 

πŸ“š Conclusion

Google Advertisement

You just created a basic file system simulation using C programming. This simple project is a great introduction to:

Understanding such simulations helps in grasping the fundamentals of Operating Systems, especially how they manage files.


πŸ”‘ Key Takeaways

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website