Basic text editor running in the console. It allows a user to open a file, display its content, edit it, and save the changes back to the file
This code defines a simple text editor that allows a user to edit text files from the console. Here's a breakdown:
1. Headers:
- : Provides functionality to use standard input/output streams.
- : Provides functionality for file input/output operations.
- : Provides the vector container.
- : Provides the string class.
2. TextEditor Class:
- Private Members:
- filename: The name of the file to be edited.
- lines: A vector that holds the lines of the file.
- Public Members & Methods:
- Constructor: Initializes filename with the passed value.
- load(): Opens and reads the file named filename, storing each line into the vector of the line.
- edit():
- Displays a message indicating the file being edited.
- Clears the vector of the existing line.
- Continuously reads lines from standard input (cin) until the user types "EXIT". Each line entered is stored in the vector of the line.
- save():
- Opens the file named filename for writing.
- Writes each line from the lines vector into the file.
- If an error opens the file for writing, it outputs an error message.
- Display (): Outputs the current content of the vector of the line to the console.
3. main() function:
- Asks the user to enter the file name they wish to edit.
- Clears the newline left in the input buffer after entering the filename.
- Creates a TextEditor object named editor with the given filename.
- Loads the content of the file (if it exists).
- Displays the current content of the file.
- Allows the user to edit the file content.
- Saves the changes back to the file.
- Confirms that the changes have been saved.
When you run this code, you can enter a filename. The program will then display its contents (if it exists), allow you to edit it, and save your changes to the file.
Comments