National University of Computer & Emerging Sciences
CS1002: Programming Fundamentals
BSCE- A (Spring 2025) A-04
[Marks: 40]
Instructor: Engr. M Hammad Lectures or Launch: Submit Date:
(
[email protected]) CH #: March 28,2025 May 09, 2025
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).
Assignment Submission: Terms & Conditions
1. This is a graded assignment; students are advised to revise all concepts before attempting.
2. Submit a single PDF or word file in GCR by the submit date mentioned in GCR; SLATE/email not
accepted.
3. Please follow this standard submission document for submission along with this title page included.
4. The screen shots of the outputs must be properly edited and cropped.
5. Any pics or images used in the PDF must be scanned with the Clear Scanner app.
6. Do not use Cam Scanner or MS Lens as it deteriorates the image quality and the writing at the
back of the page is also visible.
7. Submitting individual pictures or attaching multiple files is not accepted.
8. Late submission is not accepted.
9. Be sure to fill and check mark the agreement in the submission box. If not filled in or
checked, submission is not accepted.
Assignment Collaboration: Terms & Conditions
1. Collaboration is permitted with limitations as defined below.
2. Permitted forms of collaboration include (but not limited to) asking questions, answering questions,
explaining intent of the question, explaining concepts, highlighting methods, discussion of all types,
etc.
3. Assignment must be performed and submitted individually
4. Forbidden forms of collaboration include (but not limited to) uploading solutions or partial solutions,
letting know the partial or final answers, etc.
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04 1
Assignment
Question 1: [40 Marks (10 each part)]
Create a CLI (Command line Interface) based text edition using C++ with flowing
requirements
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).
Soft copy of report should be uploaded on GCR
CODE:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// define history size for undo/redo
#define MAX_HISTORY 10
// Arrays to store undo/redo text states
string undoHistory[MAX_HISTORY];
string redoHistory[MAX_HISTORY];
int undoIndex = 0, redoIndex = 0;
BSCE-A, spring 2025/ A-02 Page 2 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
string currentText = ""; // Holds current text being edited
// 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
// Execute option based on user's choice
switch (choice) {
case 1: createFile();
showMenu(1);
break;
case 2: addToFile(); showMenu(2); break;
case 3: readFile(); break;
case 4: emptyFile(); showMenu(4); break;
case 5: deleteFile(); break;
case 6: copyFile(); showMenu(6); break;
case 7: editFile(); break;
case 8: undo(); break;
case 9: redo(); break;
case 10: cout << "Exiting...\n"; exit(0);
default: cout << "Invalid choice!\n";
}
} while (choice != 10);
return 0;
}
// Display menu and feedback messages
void showMenu(int msg) {
system("CLS"); // Clear console screen
// Display messages based on action results
BSCE-A, spring 2025/ A-02 Page 3 of
11
Programming
Fundamentals National University Roll No:
(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: ";
}
// Create a new file; warn if file already exists
void createFile() {
string filename;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
// Check for existing file
ifstream checkFile(filename);
if (checkFile) {
cout << "Warning: A file with this name already exists. Overwrite? (Y/N): ";
char choice;
cin >> choice;
cin.ignore();
if (choice != 'Y' && choice != 'y') return;
}
// Create empty file
ofstream file(filename);
if (file) {
file.close();
cout << "\nThe file '" << filename << "' was created successfully!\n";
cout << "Press Enter to continue...";
cin.get();
} else {
cout << "Error: Could not create file!\n";
}
}
// Append user-entered text to file
BSCE-A, spring 2025/ A-02 Page 4 of
11
Programming
Fundamentals National University Roll No:
(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";
// Open file in append mode
ofstream file(filename, ios::app);
if (!file) {
showMenu(6); // File not found
return;
}
// Read input until "END"
cout << "Enter text (type END on a new line to finish):\n";
while (getline(cin, line)) {
if (line == "END") break;
file << line << "\n";
}
file.close();
}
// Read and display file content
void readFile() {
string filename, line;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
ifstream file(filename);
if (!file) {
showMenu(6);
return;
}
cout << "\nFile Contents:\n--------------\n";
while (getline(file, line)) {
cout << line << "\n";
}
file.close();
cout << "\nEnd of File. Press Enter to return to menu...";
cin.get();
}
// Clear all content from the file
void emptyFile() {
BSCE-A, spring 2025/ A-02 Page 5 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A041
string filename;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
ofstream file(filename, ios::trunc); // Truncate clears file
file.close();
cout << "\nThe file '" << filename << "' has been emptied successfully!\n";
cout << "Press Enter to continue...";
cin.get();
}
// Delete specified file from storage
void deleteFile() {
string filename;
cout << "Enter name of file: ";
getline(cin, filename);
filename += ".txt";
// Remove file from filesystem
if (remove(filename.c_str()) == 0) {
cout << "\nThe file '" << filename << "' has been deleted successfully!\n";
} else {
cout << "Error: File not found!\n";
}
cout << "Press Enter to continue...";
cin.get();
}
// Copy content from one file to another
void copyFile() {
string srcName, dstName, line;
cout << "Enter name of file to copy from: ";
getline(cin, srcName);
srcName += ".txt";
ifstream srcFile(srcName);
if (!srcFile) {
showMenu(6);
return;
}
cout << "Enter name of file to copy to: ";
getline(cin, dstName);
dstName += ".txt";
BSCE-A, spring 2025/ A-02 Page 6 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
ofstream dstFile(dstName, ios::trunc); // Overwrite destination
while (getline(srcFile, line)) {
dstFile << line << "\n";
}
srcFile.close();
dstFile.close();
cout << "\nData copied successfully!\n";
cout << "Press Enter to continue...";
cin.get();
}
// Edit entire content of a file and support undo
void editFile() {
string filename, line;
cout << "Enter name of file to edit: ";
getline(cin, filename);
filename += ".txt";
ifstream file(filename);
if (!file) {
showMenu(6);
return;
}
// Load existing text
currentText = "";
while (getline(file, line)) {
currentText += line + "\n";
}
file.close();
cout << "\nCurrent text:\n" << currentText;
cout << "\nEnter new text (type END on a new line to finish):\n";
string newText;
while (getline(cin, line)) {
if (line == "END") break;
newText += line + "\n";
}
// Save current state for undo
undoHistory[undoIndex++] = currentText;
currentText = newText;
// Write new content to file
BSCE-A, spring 2025/ A-02 Page 7 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
ofstream outFile(filename);
outFile << currentText;
outFile.close();
}
// Undo last file edit
void undo() {
if (undoIndex > 0) {
redoHistory[redoIndex++] = currentText;
currentText = undoHistory[--undoIndex];
cout << "Undo successful!\n";
} else {
cout << "No changes to undo.\n";
}
}
// Redo last undone action
void redo() {
if (redoIndex > 0) {
undoHistory[undoIndex++] = currentText;
currentText = redoHistory[--redoIndex];
cout << "Redo successful!\n";
} else {
cout << "No changes to redo.\n";
}
}
OUTPUT:
BSCE-A, spring 2025/ A-02 Page 8 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
Creating file:
Add to file:
BSCE-A, spring 2025/ A-02 Page 9 of
11
Programming
Fundamentals National University Roll No:
(CS21002)
of Computer and Emerging Sciences
Islamabad Spring 2025 A04
1
Read from file:
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:
BSCE-A, spring 2025/ A-02 Page 11 of
11