Create Simple Text Editor in C

Faraz

By Faraz -

Learn how to create a simple text editor in C with an advanced menu interface. Step-by-step guide, compiling, running the program, and more in this comprehensive tutorial.


create-simple-text-editor-in-c-programming.webp

Introduction

Creating a text editor in C programming is a great way to understand file handling, strings, and user interfaces. In this tutorial, you'll learn how to create a simple text editor with an advanced menu interface. The text editor will allow users to create, edit, and save text files directly from the console. This guide is designed to be beginner-friendly, with simple language and easy-to-follow steps, making it perfect for those new to C programming or looking to improve their skills.

Step-by-Step Guide to Create a Simple Text Editor in C

1. Setting Up the Project

First, ensure that you have a C compiler installed, such as GCC. Create a new C file in your preferred text editor or IDE (Integrated Development Environment) and name it something like text_editor.c.

2. Include the Necessary Libraries

To start, include the standard libraries that will help you with input/output operations, string handling, and file management.

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

3. Define the Menu Functions

Let's begin by creating functions for each menu option. These functions will handle the core operations of the text editor, such as creating, opening, saving, and exiting.

void createNewFile();
void openFile();
void saveFile();
void exitEditor();

4. Create the Main Menu Interface

The main menu will allow users to select different options, such as creating a new file, opening an existing file, saving the current file, and exiting the editor. Use a loop to keep the menu running until the user decides to exit.

void mainMenu() {
    int choice;
    do {
        clearScreen();
        printf("\033[1;34m"); // Set text color to blue
        printf("===========================================\n");
        printf("|            \033[1;33mTEXT EDITOR MENU\033[1;34m             |\n"); // Yellow text
        printf("===========================================\n");
        printf("| \033[1;32m1. Create New File\033[1;34m                           |\n"); // Green text
        printf("| \033[1;32m2. Open Existing File\033[1;34m                       |\n");
        printf("| \033[1;32m3. Save Current File\033[1;34m                        |\n");
        printf("| \033[1;31m4. Exit Editor\033[1;34m                              |\n"); // Red text
        printf("===========================================\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                createNewFile();
                break;
            case 2:
                openFile();
                break;
            case 3:
                saveFile();
                break;
            case 4:
                exitEditor();
                break;
            default:
                printf("\033[1;31mInvalid choice! Please try again.\033[0m\n"); // Red text
        }
        printf("\033[0m"); // Reset text color
        printf("Press Enter to continue...");
        getchar(); // Pause before returning to menu
        getchar(); // Wait for Enter key
    } while(choice != 4);
}

5. Implement the Create New File Function

This function allows users to create a new text file and input text. The text will be stored in memory until saved.

void createNewFile() {
    char text[1000];
    printf("Enter your text (end with a blank line):\n");
    getchar(); // To consume the newline character from the previous input
    fgets(text, sizeof(text), stdin);
    printf("Your text:\n%s\n", text);
}

6. Implement the Open File Function

This function opens an existing text file, reads its contents, and displays them on the screen.

void openFile() {
    char filename[100];
    FILE *file;

    printf("Enter the filename to open: ");
    scanf("%s", filename);
    file = fopen(filename, "r");

    if (file == NULL) {
        printf("File not found!\n");
        return;
    }

    char ch;
    printf("File contents:\n");
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }

    fclose(file);
}

7. Implement the Save File Function

This function saves the current text in memory to a file.

void saveFile() {
    char filename[100];
    char text[1000];
    FILE *file;

    printf("Enter filename to save: ");
    scanf("%s", filename);
    file = fopen(filename, "w");

    if (file == NULL) {
        printf("Error saving file!\n");
        return;
    }

    printf("Enter your text (end with a blank line):\n");
    getchar(); // To consume the newline character from the previous input
    fgets(text, sizeof(text), stdin);

    fprintf(file, "%s", text);
    fclose(file);
    printf("File saved successfully!\n");
}

8. Implement the Exit Function

This function exits the editor by simply printing a goodbye message.

void exitEditor() {
    printf("Exiting the editor. Goodbye!\n");
    exit(0);
}

9. Main Function to Run the Program

Finally, create the main function that will run the text editor by calling the mainMenu function.

int main() {
    mainMenu();
    return 0;
}

Compiling and Running the Program

Compiling the Program

To compile the program, use the following command in your terminal or command prompt:

gcc text_editor.c -o text_editor

This command will create an executable file named text_editor.

Running the Program

After compiling, run the program using the following command:

text_editor

The text editor will start, and you can navigate through the menu options to create, open, save, or exit.

Conclusion

In this tutorial, you've learned how to create a simple text editor in C programming with an advanced menu interface. By following the step-by-step guide, you now have a functional text editor that allows users to create, edit, and save text files. This project is a great way to enhance your understanding of file handling, string manipulation, and creating user interfaces in C. Keep experimenting with additional features like search, replace, or even a graphical interface to take your text editor to the next level.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺