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.
In this tutorial, we’ll implement a basic file system using the C programming language that allows you to:
Create a file
Write data to a file
Read data from a file
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.
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.
Our simple file system will support:
Creating a new file (with a name)
Writing text content to that file
Reading and displaying content from a file
We’ll use file handling functions in C (fopen
, fprintf
, fscanf
, etc.) to perform these operations.
#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);
}
main()
FunctionShows a menu to the user using printf
.
Takes user input for which operation to perform.
Based on choice, calls one of the functions: createFile()
, writeFile()
, or readFile()
.
createFile()
FunctionFILE *fp = fopen(filename, "w");
Opens a new file in write mode.
If the file doesn’t exist, it creates one.
If it exists, it clears existing content.
fclose(fp)
closes the file after operation.
writeFile()
FunctionFILE *fp = fopen(filename, "a");
Opens the file in append mode.
Takes input text from user using fgets()
.
Writes the input to the file using fprintf()
.
Appends content without deleting previous text.
readFile()
Functionchar ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
Opens file in read mode.
Reads each character using fgetc()
until the end of file (EOF
).
Displays content using putchar()
.
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.
=== 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 ---
You just created a basic file system simulation using C programming. This simple project is a great introduction to:
File creation
Writing data
Reading data
Menu-based user interaction
Understanding such simulations helps in grasping the fundamentals of Operating Systems, especially how they manage files.
fopen()
is used for creating and opening files.
fprintf()
and fputs()
write data to files.
fgetc()
reads characters from a file.
Always close files using fclose()
to avoid memory leaks.
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!