🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

A database management system in C++ involves creating various functionalities to manage, store, update, retrieve, and delete data from a database. File handling is used to simplify different functions, while the C++ standard library aids in processing.
Storage of records can be implemented using a class or a similar structure to represent database entries.
To save data persistently, file I/O operations are utilized.
CRUD refers to:
Providing an interface for interacting with the database management system.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
// Definition of a structure to store records
struct Record {
int id;
string name;
string email;
void display() const {
cout << setw(5) << id << setw(20) << name << setw(30) << email << endl;
}
};
// Function prototypes
void addRecord();
void viewRecords();
void updateRecord();
void deleteRecord();
// Helper functions
Record parseRecord(const string& line);
void saveRecords(const vector<Record>& records);
vector<Record> loadRecords();
int main() {
int choice;
do {
cout << "\nDatabase Management System\n";
cout << "1. Add Record\n";
cout << "2. View Records\n";
cout << "3. Update Record\n";
cout << "4. Delete Record\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addRecord(); break;
case 2: viewRecords(); break;
case 3: updateRecord(); break;
case 4: deleteRecord(); break;
case 5: cout << "Exiting...\n"; break;
default: cout << "Invalid choice. Try again.\n";
}
} while (choice != 5);
return 0;
}
void addRecord() {
Record newRecord;
cout << "Enter ID: ";
cin >> newRecord.id;
cin.ignore(); // Clear input buffer
cout << "Enter Name: ";
getline(cin, newRecord.name);
cout << "Enter Email: ";
getline(cin, newRecord.email);
ofstream file("database.txt", ios::app);
if (file) {
file << newRecord.id << "," << newRecord.name << "," << newRecord.email << "\n";
file.close();
cout << "Record added successfully.\n";
} else {
cerr << "Error opening file for writing.\n";
}
}
void viewRecords() {
vector<Record> records = loadRecords();
for (const auto& record : records) {
record.display();
}
}
void deleteRecord() {
vector<Record> records = loadRecords();
int id;
cout << "Enter the ID of the record to delete: ";
cin >> id;
auto it = remove_if(records.begin(), records.end(), [id](const Record& record) {
return record.id == id;
});
if (it != records.end()) {
records.erase(it, records.end());
saveRecords(records);
cout << "Record deleted successfully.\n";
} else {
cout << "Record not found.\n";
}
}
Record parseRecord(const string& line) {
Record record;
size_t pos1 = line.find(',');
size_t pos2 = line.rfind(',');
record.id = stoi(line.substr(0, pos1));
record.name = line.substr(pos1 + 1, pos2 - pos1 - 1);
record.email = line.substr(pos2 + 1);
return record;
}
vector<Record> loadRecords() {
vector<Record> records;
ifstream file("database.txt");
if (file) {
string line;
while (getline(file, line)) {
records.push_back(parseRecord(line));
}
file.close();
} else {
cerr << "Error opening file for reading.\n";
}
return records;
}
void saveRecords(const vector<Record>& records) {
ofstream file("database.txt", ios::trunc);
if (file) {
for (const auto& record : records) {
file << record.id << "," << record.name << "," << record.email << "\n";
}
file.close();
} else {
cerr << "Error opening file for writing.\n";
}
}
Each record's data is stored using a structure with an id
, name
, and email
.
The file database.txt
stores all records. ofstream
is used to append or truncate records.
The system provides different options for CRUD operations.
saveRecords()
with transaction support using temporary files for atomic writing.Record
struct for time-series data logging..dbf
flat files, similar to this approach but without binary optimization.This C++ DBMS is a stepping stone to more advanced database solutions.
Comments
Post a Comment