You are on page 1of 5

#include <iostream>

#include <fstream>

#include <algorithm>

#include <vector>

using namespace std;

class TStudent

public:

string Name, Surname;

int Age;

TStudent(string name, string surname, int age)

Name = name;

Surname = surname;

Age = age;

cout <<"\n";

}
string toString() const

return Name + " ; " + Surname + " ; " + to_string(Age);

int aux1 = sizeof(Name), aux2 = sizeof(Surname);

};

class TRegistru

public:

string name, surname;

int age;

vector <TStudent> students;

TRegistru(const TStudent &other)

this->name = other.Name;

this->surname = other.Surname;

this->age = other.Age;

students.push_back(other);

void adauga(const TStudent& student)

students.push_back(student);

}
void salveaza(string file_name) // crearea fisierului si salvarea datelor

ofstream file1;

file1.open(file_name, ios::app);

file1.write((char*)&students, sizeof(students));

file1.close();

cout<<"\nFile saved and closed successfully.\n"<<endl;

void sterge()

students.clear();

void incarca(string file_name) // deschiderea fisierului si citirea datelor

ifstream file2;

file2.open(file_name, ios::in);

if(!file2)

cout<<"Error in opening file..";

else

cout<<"File opened successfully.\n"<<endl;

file2.seekg(0);

file2.read((char*)&students, sizeof(students));

void afiseaza() // afisarea datelor

{
for(auto student : students)

cout << student.Name << endl ;

cout << student.Surname << endl;

cout << student.Age << endl;

cout <<"\n";

string toString() const

string ret{};

for(const auto& student : students)

ret += student.toString() + "\n";

return ret;

};

int main()

TStudent student1("Simion", "Neculae", 21);

TStudent student2("Elena", "Oprea", 21);

TRegistru registru(student1);

registru.adauga(student2);

registru.salveaza("data.bin"); // crearea fisierului si salvarea datelor in fisier

registru.sterge();

registru.incarca("data.bin"); // deschiderea fisierului si citirea datelor din fisier

registru.afiseaza();
return 0;

You might also like