Writing, reading, searching and modifying the text file, classes c ++

1

I've been a bit stuck in this, file management or file synchrony with the program, I need you to give me a hand and illustrate me about this.

In the class users as you will see I write in a text file (" users.txt ")

What I am looking for with all this is, to be able to search the file (so that it is not only for writing) to modify and show the data of the individual users, that is to say that all the entered data is saved and when executing it again are the data.

The following code is what I have been working on, I put it to understand my question better.

PERSONAL CLASS: (parent class)

Header

#ifndef PERSO_H
#define PERSO_H
#include <iostream>   
#include <string>

using namespace std;
// Atributos
class perso {
private:
    string nombre, apellido, telefono; 
public:
    string cedula;
    void regis_perso (string nom, string ape, string ced, string tel); //Registra los datos de la clase.
};
#endif /* PERSO_H */

CPP

#include "perso.h"

using namespace std;

void perso::regis_perso(string nom, string ape, string ced, string tel)
{
    nombre = nom;
    apellido = ape;
    cedula = ced;
    telefono = tel;
}

USER CLASS (inherited class)

Header

#ifndef USER_H
#define USER_H
#include <iostream>
#include <string>
#include "perso.h"

using namespace std;
//Atributos
class user: public perso{
public:
    string iduser;
    void regis_user (string nom, string ape, string ced, string tel, string idu, string fec, string pas, string sex); //Registra los datos de la clase.
private:
    string fechana, pass, sexo;
};

#endif /* USER_H */

CPP

#include "user.h"

void user::regis_user (string nom, string ape, string ced, string tel, string idu, string fec, string pas, string sex)
{
    regis_perso (nom, ced, ape, tel);
    iduser = idu;
    fechana = fec;
    pass = pas;
    sexo = sex;   
}

CLASS USERS

Header

//Clase arreglo de objeto (de user).
#ifndef USERS_H
#define USERS_H
#include <iostream>
#include <fstream>
#include <string>
#include "user.h"
#include "modulos.h"
//Atributos de la clase.
using namespace std;

class users {
public:
    bool exis_user (string ced, string idu); //Función que verifica si existe un usuario (verdadero si falso no). 
    int pos_vacia_users (); //Función que devuelve la posición vació del arreglo "usuarios".
    int pos_user (string ced, string idu); //Función que devuelve la posición del "usuario".
    void insta_users (); //Inicializa cada posición del arreglo "usuarios".
    void regis_users (); //Modulo que registra los datos de los usuario.
    user usuarios [10];
};

#endif /* USERS_H */

CPP

#include "users.h"

bool users::exis_user (string ced, string idu)
{
    bool enc;
    enc = false;
    for (int i = 0; i <= 9; i++)
    {
        if ((usuarios[i].cedula == ced) and (usuarios[i].iduser == idu))
        {
            enc = true;
        }
    }
    return (enc);
}
void users::insta_users ()
{
    for (int i = 0; i <= 9; i++)
    {
        usuarios[i].inici_user ();
    }
}
int users::pos_vacia_users ()
{
    int pos, i;
    i = 0;
    pos = 2016;
    while ((pos == 2016) and (i <= 9))
    {
        if ((usuarios[i].cedula == "VACIO") and (usuarios[i].iduser == "VACIO"))
        {
            pos = i;
        }
        i = i + 1;
    }
    return (pos);
}
int users::pos_user (string ced, string idu)
{
    int pos, i;
    i = 0;
    pos = 2016;
    while ((pos == 2016) and (i <= 9))
    {
        if ((usuarios[i].cedula == ced) and (usuarios[i].iduser == idu))
        {
            pos = i;
        }
        i = i + 1;
    }
    return (pos);
}
void users::regis_users ()
{
    string nombre, apellido, cedula, telefono, fechana, pass, sexo, iduser;
    ofstream escriuser;
    escriuser.open ("usuarios.txt", ios::out | ios::app);
    bool aux;
    int pos, resp;
    resp = 0;
    aux = false;
    resp = 0;
    if (escriuser.is_open())
    {
        while (resp == 0)
        {
            nombre = "";
            apellido = "";
            cedula = "";
            telefono = "";
            fechana = "";
            pass = "";
            sexo = "";
            iduser = "";
            pos = 0;
            pos = pos_vacia_users ();
            if (pos == 2016)
            {
                cout << "No se pueden registrar mas usuarios (registro lleno)." << endl;
                press_key ();
                resp = 1;
            }
            else
            {
                cout << "REGISTRO DE USUARIO NRO: " <<pos<< endl;
                cout << "Introduzca los siguientes datos:" << endl;
                cout << "Cedula:" << endl; //Se pueden verificar los datos independientemente
                cin >> cedula;
                cout << "Nombre de usuario (ID user):" << endl;
                cin >> iduser;
                aux = exis_user(cedula, iduser);
                while (aux == true) 
                {
                    cedula = "";
                    iduser = "";
                    cout << "Error!!! Los datos introducidos pertenecen a un usuario ya existente." << endl;
                    press_key ();
                    cout << "Intentelo nuevamente..." << endl;
                    cout << "Cedula:" << endl;
                    cin >> cedula;
                    cout << "Nombre de usuario:" << endl;
                    cin >> iduser;
                    aux = exis_user(cedula, iduser);
                }
                cout << "Nombre:" << endl;
                cin >> nombre;
                cout << "Apellido:" << endl;
                cin >> apellido;
                cout << "Telefono:" << endl;
                cin >> telefono;
                cout << "Fecha de nacimiento:" << endl;
                cin >> fechana;
                cout << "Contrase;a:" << endl;
                cin >> pass;
                cout << "Sexo:" << endl;
                cin >> sexo;
                usuarios[pos].regis_user(nombre, apellido, cedula, telefono, iduser, fechana, pass, sexo);
                escriuser<<nombre<<" "<<apellido<<" "<<cedula<<" "<<telefono<<" "<<iduser<<" "<<fechana<<" "<<pass<<" "<<sexo<<endl;
                cout << "¿Desea registrar otro usuario?" << endl;
                cout << "(0./ SI, 1./NO)" << endl;
                cin >> resp;
                while ((resp != 1) and (resp != 2))
                {
                    cout << "Error!!! Respuesta invalida." << endl;
                    press_key ();
                    cout << "Intentelo nuevamente." << endl;
                    cin >> resp;
                }
            }
        }
    }
    else
    {
        cout << "Error!!! el archivo no pudo ser abierto." << endl;
    }
    escriuser.close ();
}

It would be great if your answers gave me recommendations, examples or suggestions, to help me in this.

    
asked by Cookie Rabbit 10.01.2017 в 09:11
source

0 answers