Build a File Management System in C Language

Table of Contents

Introduction – File System

In the Article, we are going to create a File Management System in C Language. This system allow users to create, read, write and delete files using a menu-driven interfaces. And this Project is the boost the logic knowledge of C Language.

Output – File System

Structure of the Project

In the structure of the project code section you get complete explanation about the project codes,

Part – 1 Header Files and Function Prototype

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

void createFile(char *filename);
void readFile(char *filename);
void writeFile(char *filename);
void deleteFile(char *filename);
  • The ‘stdio,h’ header file is included for standard input and output operations.
  • The ‘stdlib.h’ header file is included for the ‘exit()’ function.
  • Function Prototype are declared for the handling functions, indicating their signatures.
C Language Free Notes

Part – 2 File Handling Functions

Here are the three File handling Functions which we used in our project.

a. ‘createFile (char *filename)’
void createFile(char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error: Unable to create file.\n");
        return;
    }
    fclose(file);
    printf("File '%s' created successfully.\n", filename);
}
  • The function creates a new file with the specified filename.
  • It uses ‘fopen()’ to open the file in write mode (“w”).
  • If the file Pointer is ‘NULL’, it indicates an error in the file creation.
  • Otherwise, it closes the file using ‘fclose()’ and display a success massage.
b. ‘readFile(char *filename)’
void readFile(char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error: Unable to open file.\n");
        return;
    }
    char ch;
    printf("Contents of file '%s':\n", filename);
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
    fclose(file);
}
  • This function reads the contents of an existing file.
  • It uses “fopen()’ to open the file in read mode (“r”).
  • If the file pointer is ‘NULL’, it indicates an error in file opening.
  • It read characters from the file using ‘fgetc()’ until ‘EOF’ (end of file) is reached.
  • It closes the file using ‘fclose()’ after reading.
c. ‘writeFile (char *filename)’
void writeFile(char *filename) {
    FILE *file = fopen(filename, "a");
    if (file == NULL) {
        printf("Error: Unable to open file.\n");
        return;
    }
    printf("Enter text to write to file (press Ctrl+D to end):\n");
    char ch;
    while ((ch = getchar()) != EOF) {
        fputc(ch, file);
    }
    fclose(file);
    printf("Text written to file '%s' successfully.\n", filename);
}
  • This function appends text to an existing file.
  • it uses “fopen” to open the file in append mode (“a”).
  • if the File Pointer is ‘NULL’, it indicates an error in file opening.
  • It reads characters from the standard input (‘getchar()’) until “EOF” is reached.
  • it writes characters to the file using “fputc()”.
  • It closes the file after writing and displays a success message.
d. ‘deleteFile(char *filename)’
void deleteFile(char *filename) {
    if (remove(filename) == 0) {
        printf("File '%s' deleted successfully.\n", filename);
    } else {
        printf("Error: Unable to delete file.\n");
    }
}
  • The Function deletes an existing file.
  • It uses the “remove()” function to delete the file.
  • If deletion is successful (‘remove()’ returns ‘0’), it displays a success message.
  • Otherwise, it indicates an error in file deletion.

Part – 3 Main Function

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

    // Display menu
    printf("Simple File Management System\n");
    printf("1. Create File\n");
    printf("2. Read File\n");
    printf("3. Write to File\n");
    printf("4. Delete File\n");
    printf("5. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter filename to create: ");
            scanf("%s", filename);
            createFile(filename);
            break;
        case 2:
            printf("Enter filename to read: ");
            scanf("%s", filename);
            readFile(filename);
            break;
        case 3:
            printf("Enter filename to write: ");
            scanf("%s", filename);
            writeFile(filename);
            break;
        case 4:
            printf("Enter filename to delete: ");
            scanf("%s", filename);
            deleteFile(filename);
            break;
        case 5:
            printf("Exiting...\n");
            exit(0);
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}
  • This main function is serves as the entry point of the program.
  • It displays a menu of options for file management operations.
  • It reads the user’s choice using ‘scanf()’ and performs the corresponding operations.
  • Depending on the user’s choice, it calls the appropriate file handling function.
  • The program exits gracefully if the user chooses to exit.

Complete Codes of Project

Here is the complete codes of the Project File Management System which we creates in C Language,

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

void createFile(char *filename) {
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error: Unable to create file.\n");
        return;
    }
    fclose(file);
    printf("File '%s' created successfully.\n", filename);
}

void readFile(char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error: Unable to open file.\n");
        return;
    }
    char ch;
    printf("Contents of file '%s':\n", filename);
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
    fclose(file);
}

void writeFile(char *filename) {
    FILE *file = fopen(filename, "a");
    if (file == NULL) {
        printf("Error: Unable to open file.\n");
        return;
    }
    printf("Enter text to write to file (press Ctrl+D to end):\n");
    char ch;
    while ((ch = getchar()) != EOF) {
        fputc(ch, file);
    }
    fclose(file);
    printf("Text written to file '%s' successfully.\n", filename);
}

void deleteFile(char *filename) {
    if (remove(filename) == 0) {
        printf("File '%s' deleted successfully.\n", filename);
    } else {
        printf("Error: Unable to delete file.\n");
    }
}

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

    printf("Simple File Management System\n");
    printf("1. Create File\n");
    printf("2. Read File\n");
    printf("3. Write to File\n");
    printf("4. Delete File\n");
    printf("5. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter filename to create: ");
            scanf("%s", filename);
            createFile(filename);
            break;
        case 2:
            printf("Enter filename to read: ");
            scanf("%s", filename);
            readFile(filename);
            break;
        case 3:
            printf("Enter filename to write: ");
            scanf("%s", filename);
            writeFile(filename);
            break;
        case 4:
            printf("Enter filename to delete: ");
            scanf("%s", filename);
            deleteFile(filename);
            break;
        case 5:
            printf("Exiting...\n");
            exit(0);
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}

Conclusion

So In this Articles, we completely learned about the How File Management System created in C Language. And how the logics works in this for creating a File Management System in C.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top