If you are using C ++ the most natural thing is to use the fstream library to manage the files and replace the character arrays by string, unless a fixed character size is mandatory.
Personally I do not recommend combining the logic of the program with the "Key Objects" in this case the Person class should only handle the data of the person in turn and not the registration or filling the latter should be done in another section of the program.
A possible implementation of the Persona class would be like this.
class Persona
{
private:
string nombre;
string apellido;
string cedula;
public:
Persona(const string& nombre, const string& apellido, const string& cedula)
{
this->nombre=nombre;
this->apellido=apellido;
this->cedula=cedula;
}
//Implementacion
string getNombre()
{
return this->nombre;
}
string getApellido()
{
return this->apellido;
}
string getCedula()
{
return this->cedula;
}
};
Then you could create an Agenda class, I do not know if in your case it's one but it's an idea you could handle the data of the people.
Well in my case I create a class called Agenda where to manage people's records and will act as a kind of database, in which new records can be added or consult existing ones.
class Agenda
{
private:
fstream file;
vector<string> split(const string& source,char token);
bool fileExists(const string& ruta);
public:
Agenda(const string& ruta);
void agregar(Persona& persona);
void agregarTodas(const vector<Persona>& personas);
vector<Persona> leerTodo();
~Agenda();//Destructor de la clase
};
Agenda::Agenda(const string& ruta)
{
if(fileExists(ruta))
{
file.open(ruta.c_str(),fstream::in | fstream::out | fstream::app);
}
else
{
file.open(ruta.c_str(),fstream::in | fstream::out | fstream::trunc);
}
}
vector<string> Agenda::split(const string& source,char token)
{
std::string sentence="";
vector<string> result;
for(char c :source)
{
if(c==token)
{
result.push_back(sentence);
sentence="";
continue;
}
sentence.push_back(c);
}
return result;
}
bool Agenda::fileExists(const string& ruta)
{
fstream infile(ruta.c_str());
return infile.good();
}
void Agenda::agregar(Persona& persona)
{
if(file.is_open())
{
file<<persona.getNombre()<<","<<persona.getApellido()<<","<<persona.getCedula()<<","<<endl;
}
}
void Agenda::agregarTodas(const vector<Persona>& personas)
{
if(file.is_open())
{
for(Persona p: personas)
{
file<<p.getNombre()<<","<<p.getApellido()<<","<<p.getCedula()<<","<<endl;
}
}
}
vector<Persona> Agenda::leerTodo()
{
vector<Persona> personas;
if(file.good())
{
file.seekg(0,file.beg);//Nos ponemos al inicio del fichero
string linea;
while(getline(file,linea))
{
vector<string> tokens=split(linea,',');
string nombre=tokens[0];
string apellido=tokens[1];
string cedula=tokens[2];
Persona p(nombre,apellido,cedula);
personas.push_back(p);
}
}
return personas;
}
Agenda::~Agenda()
{
file.close();
}
A start-up of the two classes working together would be this:
int main()
{
//Creamos el objeto Agenda si el archivo no existe se crea, si existe se anade datos a la agenda
Agenda agenda("C:\Users\usuario\Documents\agenda.txt");
//Agregamos 1 persona
Persona p=Persona("luis","arturo","0435845");
agenda.agregar(p);
vector<Persona> personas;
personas.push_back(Persona("Mari","Perez","34353543"));
personas.push_back(Persona("Jose","Arturo","9343533"));
agenda.agregarTodas(personas);
vector<Persona> datosLeidos=agenda.leerTodo();
for(Persona p:datosLeidos)
{
cout<<p.getNombre()<<" "<<p.getApellido()<<" "<<p.getCedula()<<endl;
}
cout<<"\nPrograma terminado"<<endl;
return 0;
}
In it we can see that the class Agenda is responsible for managing the creation and reading of our file, if the file does not exist it creates it and if it already exists it only adds records at the end, at the same time that it accesses the data.