#include\x20 #include\x20 #include\x20 #include\x20

using\x20namespace\x20std;

struct\x20Record\x20{ \x20\x20string\x20name; \x20\x20int\x20age; \x20\x20string\x20address; };

class\x20Database\x20{ private: \x20\x20vector\x20records; \x20\x20string\x20filename; \x20\x20 \x20\x20void\x20saveToFile()\x20{ \x20\x20\x20\x20ofstream\x20file(filename); \x20\x20\x20\x20if\x20(file.is_open())\x20{ \x20\x20\x20\x20\x20\x20for\x20(const\x20auto&\x20record\x20:\x20records)\x20{ \x20\x20\x20\x20\x20\x20\x20\x20file\x20<<\x20record.name\x20<<\x20","\x20<<\x20record.age\x20<<\x20","\x20<<\x20record.address\x20<<\x20endl; \x20\x20\x20\x20\x20\x20} \x20\x20\x20\x20\x20\x20file.close(); \x20\x20\x20\x20\x20\x20cout\x20<<\x20"Database\x20saved\x20to\x20file.\n"; \x20\x20\x20\x20}else\x20{ \x20\x20\x20\x20\x20\x20cout\x20<<\x20"Error\x20opening\x20file.\n"; \x20\x20\x20\x20} \x20\x20} \x20\x20 \x20\x20void\x20loadFromFile()\x20{ \x20\x20\x20\x20ifstream\x20file(filename); \x20\x20\x20\x20if\x20(file.is_open())\x20{ \x20\x20\x20\x20\x20\x20string\x20line; \x20\x20\x20\x20\x20\x20while\x20(getline(file,\x20line))\x20{ \x20\x20\x20\x20\x20\x20\x20\x20Record\x20record; \x20\x20\x20\x20\x20\x20\x20\x20size_t\x20commaIndex\x20=\x20line.find(","); \x20\x20\x20\x20\x20\x20\x20\x20record.name\x20=\x20line.substr(0,\x20commaIndex); \x20\x20\x20\x20\x20\x20\x20\x20line\x20=\x20line.substr(commaIndex\x20+\x201); \x20\x20\x20\x20\x20\x20\x20\x20 \x20\x20\x20\x20\x20\x20\x20\x20commaIndex\x20=\x20line.find(","); \x20\x20\x20\x20\x20\x20\x20\x20record.age\x20=\x20stoi(line.substr(0,\x20commaIndex)); \x20\x20\x20\x20\x20\x20\x20\x20line\x20=\x20line.substr(commaIndex\x20+\x201); \x20\x20\x20\x20\x20\x20\x20\x20 \x20\x20\x20\x20\x20\x20\x20\x20record.address\x20=\x20line; \x20\x20\x20\x20\x20\x20\x20\x20records.push_back(record); \x20\x20\x20\x20\x20\x20} \x20\x20\x20\x20\x20\x20file.close(); \x20\x20\x20\x20\x20\x20cout\x20<<\x20"Database\x20loaded\x20from\x20file.\n"; \x20\x20\x20\x20}else\x20{ \x20\x20\x20\x20\x20\x20cout\x20<<\x20"Error\x20opening\x20file.\n"; \x20\x20\x20\x20} \x20\x20} \x20\x20 public: \x20\x20Database(const\x20string&\x20filename)\x20:\x20filename(filename)\x20{ \x20\x20\x20\x20loadFromFile(); \x20\x20} \x20\x20 \x20\x20void\x20addRecord(const\x20Record&\x20record)\x20{ \x20\x20\x20\x20records.push_back(record); \x20\x20\x20\x20saveToFile(); \x20\x20} \x20\x20 \x20\x20void\x20printRecords()\x20const\x20{ \x20\x20\x20\x20for\x20(const\x20auto&\x20record\x20:\x20records)\x20{ \x20\x20\x20\x20\x20\x20cout\x20<<\x20"Name:\x20"\x20<<\x20record.name\x20<<\x20",\x20Age:\x20"\x20<<\x20record.age\x20<<\x20",\x20Address:\x20"\x20<<\x20record.address\x20<<\x20endl; \x20\x20\x20\x20} \x20\x20} };

int\x20main()\x20{ \x20\x20Database\x20db("data.txt"); \x20\x20 \x20\x20Record\x20record1; \x20\x20record1.name\x20=\x20"John"; \x20\x20record1.age\x20=\x2025; \x20\x20record1.address\x20=\x20"123\x20Main\x20St"; \x20\x20db.addRecord(record1); \x20\x20 \x20\x20Record\x20record2; \x20\x20record2.name\x20=\x20"Amy"; \x20\x20record2.age\x20=\x2030; \x20\x20record2.address\x20=\x20"456\x20Elm\x20St"; \x20\x20db.addRecord(record2); \x20\x20 \x20\x20db.printRecords(); \x20\x20 \x20\x20return\x200;

C++ Simple Database Implementation - Save and Load Data to File

原文地址: https://www.cveoy.top/t/topic/pPMx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录