1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include <iostream> #include <fstream> #include <string> using namespace std; // Function to display the menu void displayMenu() { cout << "Text Editor Menu:" << endl; cout << "1. Create a new file" << endl; cout << "2. Open an existing file" << endl; cout << "3. Save the current file" << endl; cout << "4. Edit text" << endl; cout << "5. Exit" << endl; cout << "Choose an option: "; } // Function to create a new file void createNewFile(string& fileName, string& fileContent) { cout << "Enter the new file name: "; cin >> fileName; fileContent.clear(); // Clear any existing content cout << "New file '" << fileName << "' created." << endl; } // Function to open an existing file void openFile(string& fileName, string& fileContent) { cout << "Enter the file name to open: "; cin >> fileName; ifstream inFile(fileName); if (inFile.is_open()) { string line; fileContent.clear(); while (getline(inFile, line)) { fileContent += line + "\n"; } inFile.close(); cout << "File '" << fileName << "' opened successfully." << endl; } else { cout << "Unable to open file '" << fileName << "'." << endl; } } // Function to save the current file void saveFile(const string& fileName, const string& fileContent) { ofstream outFile(fileName); if (outFile.is_open()) { outFile << fileContent; outFile.close(); cout << "File '" << fileName << "' saved successfully." << endl; } else { cout << "Unable to save file '" << fileName << "'." << endl; } } // Function to edit text void editText(string& fileContent) { cout << "Enter text (type 'SAVE' on a new line to stop editing):" << endl; string line; while (true) { getline(cin, line); if (line == "SAVE") break; fileContent += line + "\n"; } } int main() { string fileName; string fileContent; int choice; while (true) { displayMenu(); cin >> choice; cin.ignore(); // Ignore the newline after the choice input switch (choice) { case 1: createNewFile(fileName, fileContent); break; case 2: openFile(fileName, fileContent); break; case 3: saveFile(fileName, fileContent); break; case 4: editText(fileContent); break; case 5: cout << "Exiting the text editor." << endl; return 0; default: cout << "Invalid choice. Please try again." << endl; } } return 0; } |
Explanation
- Menu Display:
displayMenu()
: This function shows the available options: creating a new file, opening an existing file, saving the current file, editing text, and exiting the program.
- File Operations:
- Create a New File:
createNewFile()
: This function prompts the user for a new file name and clears any existing content, preparing the editor for new input.
- Open an Existing File:
openFile()
: This function asks for a file name and attempts to open it. If successful, it reads the file’s content into thefileContent
string.
- Save the Current File:
saveFile()
: This function writes the current content stored infileContent
to the file specified byfileName
.
- Create a New File:
- Text Editing:
editText()
: This function allows the user to enter text line by line. It appends each line tofileContent
until the user types “SAVE,” which signals the end of editing.
- Main Function:
- The
main()
function manages the text editor’s workflow. It continuously displays the menu, handles user input, and calls the appropriate functions based on the user’s choice.
- The