PF Assignment 04 Text editor
PF Assignment 04 Text editor
CLO 2: To use file handling in C++ and Develop modular code using functions to
efficiently solve programming problems.
Submit the softcopy on GCR by the deadline – Late Assignments are not
accepted. Submit Hardcopy as well
Submitted by: Owaim Bin Atif , Ashnab safdar, Roll #: 24i-6502,24i-6500 Section: CE-A
Eesha Rehan 24i-6027
Check here: I agree that there is a ZERO Tolerance Policy for plagiarism and cheating in all
assessments. First plagiarism case gets zero. Subsequent plagiarism cases get
ZERO in all assignments. A gross violation may be reported to the Department
Discipline Committee (DDC).
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04 1
Assignment
Multiple File Operations: Allow the user to open, edit, and save multiple files in sequence
without restarting the editor.
File Overwrite Warning: If a file is already opened, and the user attempts to save it under a
different name, prompt a warning about overwriting.
Text Navigation: Enable cursor control to move the cursor around within the text area
(up/down/left/right).
Undo/Redo: Implement a basic undo operation to revert the last change (storing previous
versions of text).
Submission Requirements:
1. Source Code: Provide the full C++ source code for the text editor.
2. Documentation: Document each feature with comments and provide a brief explanation
of the implemented functionalities.
3. Test Cases: Test the editor with different text files and ensure all features work
correctly (e.g., creating, saving, opening, undo/redo).
CODE:
#include <iostream>
#include <fstream>
#include <string>
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
// Function Declarations
void showMenu(int msg); // Display menu and messages
void createFile(); // Create a new file
void addToFile(); // Append text to a file
void readFile(); // Read file contents
void emptyFile(); // Empty file contents
void deleteFile(); // Delete a file
void copyFile(); // Copy one file to another
void editFile(); // Edit entire file content
void undo(); // Undo last edit
void redo(); // Redo previously undone edit
int main() {
int choice;
do {
showMenu(0); // Show menu without message
cin >> choice;
cin.ignore(); // Clear newline from input buffer
return 0;
}
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A041
switch (msg) {
case 1: cout << "File Created Successfully\n\n"; break;
case 2: cout << "File Updated Successfully\n\n"; break;
case 4: cout << "File Emptied Successfully\n\n"; break;
case 5: cout << "File Deleted Successfully\n\n"; break;
case 6: cout << "File Not Found\n\n"; break;
case 7: cout << "File Copied Successfully\n\n"; break;
default: cout << "Welcome to Text Editor\n\n"; break;
}
// Menu options
cout << "Main Menu\n-------------\n";
cout << "1. Create File\n2. Add to File\n3. Read from File\n4. Empty File\n";
cout << "5. Delete File\n6. Copy File\n7. Edit File\n8. Undo\n9. Redo\n10. Exit\n\n";
cout << "Enter Choice: ";
}
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
void addToFile() {
string filename, line;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
ifstream file(filename);
if (!file) {
showMenu(6);
return;
}
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A041
string filename;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
cout << "\nThe file '" << filename << "' has been emptied successfully!\n";
cout << "Press Enter to continue...";
cin.get();
}
ifstream srcFile(srcName);
if (!srcFile) {
showMenu(6);
return;
}
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
srcFile.close();
dstFile.close();
ifstream file(filename);
if (!file) {
showMenu(6);
return;
}
string newText;
while (getline(cin, line)) {
if (line == "END") break;
newText += line + "\n";
}
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
ofstream outFile(filename);
outFile << currentText;
outFile.close();
}
OUTPUT:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
Creating file:
Add to file:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
Empty file:
BSCE-A, spring 2025/ A-02 Page 10 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
Delete file:
Copy file:
File: