Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<fstream>
- const char FILE_NAME[] = "file.txt";
- class Entity {
- std::string name;
- size_t id;
- std::string password;
- public:
- Entity() { name = ""; id = 0; password = ""; }
- Entity(const std::string& _name, size_t id, const std::string& _pass) { name = _name; this->id = id; password = _pass; }
- std::string getPass() const { return this->password; }
- std::string getName() const { return this->name; }
- size_t getID() const { return this->id; }
- ~Entity() { };
- };
- void saveEntityToFile(const char* location, const Entity& toSave) {
- std::ofstream outFile(location, std::ios::app);
- if (outFile.bad())
- return;
- outFile << toSave.getName() << " " << toSave.getID() << " " << toSave.getPass() << '\n';
- outFile.close();
- }
- Entity readEntityFromFile(const char* location) {
- std::ifstream inFile(location);
- if (inFile.bad())
- return Entity();
- std::string nName;
- std::string nPass;
- size_t ID;
- inFile >> nName >> ID >> nPass;
- inFile.close();
- return Entity(nName, ID, nPass);
- }
- int main() {
- Entity test("Entity1", 1234, "Password");
- saveEntityToFile(FILE_NAME, test);
- Entity readedEntity = readEntityFromFile(FILE_NAME);
- std::cout << readedEntity.getName() << " " << readedEntity.getID() << " " << readedEntity.getPass() << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement