1.
void displayMenu()
void displayMenu(): Declares the function displayMenu with no return type or
parameters.
cout << "Menu:" << endl;: Prints "Menu:" followed by a newline.
cout << "1. Add to File" << endl;: Prints option 1 for adding text to the file.
cout << "2. View File" << endl;: Prints option 2 for viewing the file content.
cout << "3. Overwrite File" << endl;: Prints option 3 for overwriting the file.
cout << "4. Clear File" << endl;: Prints option 4 for clearing the file content.
cout << "5. Search in File" << endl;: Prints option 5 for searching text in the
file.
cout << "6. Exit" << endl;: Prints option 6 for exiting the program.
cout << "Enter your choice: ";: Asks the user to input a choice from the menu.
2. void addToFile(const string& filename)
ofstream file(filename.c_str(), ios::app);: Opens the file in append mode, allowing
new content to be added without deleting existing content.
if (file): Checks if the file was successfully opened.
string text;: Declares a string variable to store user input.
cout << "Enter text and type 'END' to stop...":: Prompts the user to input text.
while (true): Starts an infinite loop to continuously accept user input.
getline(cin, text);: Reads an entire line of input from the user.
size_t endPosition = [Link]("END");: Searches for the substring "END" in the
user input and stores its position.
if (endPosition != string::npos): Checks if "END" is found in the input.
if (endPosition > 0): Ensures any text before "END" is added to the file.
file << [Link](0, endPosition) << endl;: Writes the portion of the input
before "END" to the file.
break;: Exits the loop.
file << text << endl;: Writes the entire line to the file if "END" is not found.
[Link]();: Closes the file after writing.
cout << "Text added to " << filename << "." << endl;: Informs the user that the
text has been successfully added.
else: Executes if the file cannot be opened.
cout << "Error: Could not open the file." << endl;: Displays an error message.
3. void viewFile(const string& filename)
ifstream file(filename.c_str());: Opens the file in read mode.
if (file): Checks if the file was successfully opened.
string line;: Declares a string variable to hold each line of text from the file.
cout << "Contents of " << filename << ":" << endl;: Displays a heading for the file
content.
while (getline(file, line)): Reads the file line by line until the end is reached.
cout << line << endl;: Outputs each line to the console.
[Link]();: Closes the file after reading.
else: Executes if the file cannot be opened.
cout << "Error: File does not exist or cannot be opened." << endl;: Displays an
error message.
4. void overwriteFile(const string& filename)
ofstream file(filename.c_str());: Opens the file in write mode, erasing its
existing content.
if (file): Checks if the file was successfully opened.
cout << "Enter new content (type 'END' to stop):":: Prompts the user to input new
content.
string text;: Declares a string variable to store user input.
while (true): Starts an infinite loop to continuously accept user input.
getline(cin, text);: Reads an entire line of input from the user.
size_t endPosition = [Link]("END");: Searches for the substring "END" in the
user input and stores its position.
if (endPosition != string::npos): Checks if "END" is found in the input.
if (endPosition > 0): Ensures any text before "END" is added to the file.
file << [Link](0, endPosition) << endl;: Writes the portion of the input
before "END" to the file.
break;: Exits the loop.
file << text << endl;: Writes the entire line to the file if "END" is not found.
[Link]();: Closes the file after writing.
cout << "File content overwritten in " << filename << "." << endl;: Informs the
user that the file content has been successfully overwritten.
else: Executes if the file cannot be opened.
cout << "Error: Could not open the file for writing." << endl;: Displays an error
message.
5. void clearFile(const string& filename)
ofstream file(filename.c_str());: Opens the file in write mode, clearing its
existing content.
if (file): Checks if the file was successfully opened.
[Link]();: Immediately closes the file after clearing its content.
cout << "File content cleared." << endl;: Informs the user that the file content
has been cleared.
else: Executes if the file cannot be opened.
cout << "Error: Could not open the file." << endl;: Displays an error message.
6. void searchFile(const string& filename)
ifstream file(filename.c_str());: Opens the file in read mode.
if (file): Checks if the file was successfully opened.
string searchTerm;: Declares a string variable to store the search term.
string line;: Declares a string variable to hold each line of text from the file.
int lineNum = 0;: Initializes a line counter to track line numbers.
bool found = false;: Initializes a flag to indicate whether the search term is
found.
cout << "Enter text to search for: ";: Prompts the user to enter a search term.
getline(cin, searchTerm);: Reads the search term from the user.
cout << "Search results:" << endl;: Displays a heading for the search results.
while (getline(file, line)): Reads the file line by line.
lineNum++;: Increments the line counter for each line.
if ([Link](searchTerm) != string::npos): Checks if the search term is found in
the current line.
cout << "Line " << lineNum << ": " << line << endl;: Displays the matching line and
its number.
found = true;: Sets the flag to true if a match is found.
[Link]();: Closes the file after searching.
if (!found): Checks if no matches were found.
cout << "No matches found for \"" << searchTerm << "\" in the file." << endl;:
Displays a message indicating no matches.
else: Executes if the file cannot be opened.
cout << "Error: File does not exist or cannot be opened." << endl;: Displays an
error message.