last posts

C++ Simple Text Editor Project

Creating a simple text editor is an excellent project for beginners to gain experience with file handling and basic user interface design in C++. Here's a detailed breakdown of how to approach building a simple text editor:

Project Simple Text Editor using C++


 

Description:

Develop a basic console-based text editor that allows users to create, edit, save, and load text files. The editor should support common text manipulation features like inserting text, deleting text, and saving changes to files.

Features:
1.Text Display: Display the contents of a text file on the console.
2.Editing: Implement basic editing features like inserting, deleting, and modifying text within the editor.
3.File Handling: Allow users to open and save text files.
4.User Interface: Create a simple text-based menu for users to interact with.
Implementation Steps:
1.User Interface:
    *Display a menu that presents options to the user, such as "Open File," "Edit Text," "Save File," and "Exit."
2.File Handling:
    *Implement functions to open and save text files.
    *Use file streams (ifstream for reading and ofstream for writing) to perform file operations.
    *Prompt the user for the file name when opening or saving.
3.Text Editing:
    *Load the contents of the file into memory for editing.
     *Allow users to insert text at a specified position, delete text, and modify existing text.
    *Provide a way to navigate through the text, such as moving the cursor.
4.Displaying Text:
    *Display the text on the console, updating it as the user edits.
    *Keep track of the cursor position within the text.
5.User Input:
    *Use cin to get user input for menu choices, text editing commands, and file names.
6.Error Handling:
    *Implement error handling for file operations, such as handling cases where the file doesn't exist or cannot be saved.

Sample Code of Test Editor in C++

Here's a simplified example of a basic text editor in C++:


#include 
#include 
#include 

int main() {
    std::string fileName;
    std::string text;
    char choice;

    while (true) {
        std::cout << "Simple Text Editor" << std::endl;
        std::cout << "1. Open File" << std::endl;
        std::cout << "2. Edit Text" << std::endl;
        std::cout << "3. Save File" << std::endl;
        std::cout << "4. Exit" << std::endl;
        std::cout << "Enter your choice: ";
        std::cin >> choice;

        switch (choice) {
            case '1': {
                std::cout << "Enter file name: ";
                std::cin >> fileName;

                std::ifstream file(fileName);

                if (!file.is_open()) {
                    std::cout << "File not found." << std::endl;
                } else {
                    text = "";
                    std::string line;
                    while (std::getline(file, line)) {
                        text += line + "\n";
                    }
                    file.close();
                    std::cout << "File loaded." << std::endl;
                }
                break;
            }
            case '2': {
                if (text.empty()) {
                    std::cout << "No file is open. Please open a file first." << std::endl;
                } else {
                    std::cout << "Enter text to edit (type 'exit' to finish editing):" << std::endl;
                    std::string newText;
                    std::cin.ignore(); // Consume newline left in the input buffer
                    while (true) {
                        std::getline(std::cin, newText);
                        if (newText == "exit") {
                            break;
                        }
                        text += newText + "\n";
                    }
                    std::cout << "Text edited." << std::endl;
                }
                break;
            }
            case '3': {
                if (text.empty()) {
                    std::cout << "No text to save." << std::endl;
                } else {
                    std::cout << "Enter file name to save: ";
                    std::cin >> fileName;

                    std::ofstream file(fileName);
                    file << text;
                    file.close();
                    std::cout << "File saved." << std::endl;
                }
                break;
            }
            case '4':
                std::cout << "Exiting the text editor." << std::endl;
                return 0;
            default:
                std::cout << "Invalid choice. Please try again." << std::endl;
        }
    }
}

*This example provides a basic structure for a text editor. You can expand upon it by adding more advanced text editing features and error handling.
Commentaires



Font Size
+
16
-
lines height
+
2
-