C++ Projects: File Handling System
Hello everyone! Today, we’ll explore a project that reads from and writes to text files, making it useful for applications like a diary or a logbook.
Features
This project allows users to:
- Add new entries
- View all entries
- Search for keyword-based entries
- Delete entries
Adding Entries
When a user writes an entry, it is automatically saved in a text file.
Viewing Entries
All entries present in the text file can be displayed.
Searching Entries
Users can find entries based on keywords.
Deleting Entries
Entries can be erased by selecting an entry number.
Skills Used
File Handling
- ofstream: Writes data to files.
- ifstream: Reads data from files.
- ios::app: Opens a file in append mode to prevent overwriting.
Entry Management
Each entry is separated by -----
to maintain structure and readability.
Functions
- addEntry: Saves user input into the file.
- viewEntries: Reads and displays file content.
- searchEntry: Searches for keywords within entries.
- deleteEntry: Removes an entry by rewriting the file without it.
Menu System
A loop-based menu allows users to choose different actions until they decide to exit.
The Code
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// Function to add a new entry
void addEntry() {
ofstream outFile("diary.txt", ios::app); // Open file in append mode
if (!outFile) {
cout << "Error: could not open file.\n";
return;
}
cout << "You can write your diary entry (type 'END' on a new line to finish):\n";
string entry, line;
while (getline(cin, line)) {
if (line == "END") break;
entry += line + "\n";
}
outFile << entry << "-----\n"; // Use "-----" to separate entries
outFile.close();
cout << "Entry saved successfully.\n";
}
// Function to view all entries
void viewEntries() {
ifstream inFile("diary.txt");
if (!inFile) {
cout << "Error: could not open file.\n";
return;
}
cout << "\nAll entries:\n====================\n";
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
// Function to search for an entry
void searchEntry() {
ifstream inFile("diary.txt");
if (!inFile) {
cout << "Error: could not open file.\n";
return;
}
cout << "Enter keyword to search: ";
string keyword;
cin.ignore();
getline(cin, keyword);
string line, entry;
bool found = false;
while (getline(inFile, line)) {
if (line == "-----") {
if (entry.find(keyword) != string::npos) {
cout << "\nFound Entry:\n====================\n" << entry << "====================\n";
found = true;
}
entry.clear();
} else {
entry += line + "\n";
}
}
if (!found) cout << "No entry found with the keyword " << keyword << "\n";
inFile.close();
}
// Function to delete an entry
void deleteEntry() {
ifstream inFile("diary.txt");
if (!inFile) {
cout << "Error: could not open file.\n";
return;
}
vector<string> entries;
string line, entry;
while (getline(inFile, line)) {
if (line == "-----") {
entries.push_back(entry);
entry.clear();
} else {
entry += line + "\n";
}
}
inFile.close();
cout << "\nEntries:\n";
for (size_t i = 0; i < entries.size(); i++) {
cout << i + 1 << ".\n" << entries[i] << "====================\n";
}
cout << "Enter the entry number to delete: ";
int choice;
cin >> choice;
if (choice < 1 || choice > entries.size()) {
cout << "Invalid choice.\n";
return;
}
entries.erase(entries.begin() + choice - 1);
ofstream outFile("diary.txt");
for (const auto& e : entries) {
outFile << e << "-----\n";
}
outFile.close();
cout << "Entry deleted successfully.\n";
}
// Main Menu
int main() {
int choice;
do {
cout << "\nEntry Management System\n";
cout << "1. Add entry\n";
cout << "2. View entries\n";
cout << "3. Search entry\n";
cout << "4. Delete entry\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1: addEntry(); break;
case 2: viewEntries(); break;
case 3: searchEntry(); break;
case 4: deleteEntry(); break;
case 5: cout << "Exiting the program.\n"; break;
default: cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Real-World Applications
Diary Writing
- Users can record their thoughts daily.
- Search functionality allows retrieving past experiences using keywords like “holiday” or “meeting.”
Employee Logbook
- Tracks attendance and work hours efficiently.
- Enhancements can include employee IDs and timestamps.
Bug Tracking System
- Developers log issues with timestamps.
- Entries can be categorized with tags like “UI Bug.”
- Instead of deleting entries, marking them as “resolved” improves tracking.
Expense Tracker
- Users can categorize expenses, e.g., “Food,” “Travel.”
- Search functionality filters expenses by date or category.
- Summarizing expenses over a period helps in financial planning.
This project is a great way to practice C++ file handling while building something useful. Hope you found this blog insightful—see you next time!
Comments
Post a Comment