Does not save or search data in the C ++ file

1

The most detailed problem is that when I enter a new student I save it, I look for it and it shows it, but as soon as I enter the second student onwards he throws "Error: DNI Incorrect." Touch any button to return to the previous menu " I do not know if it keeps a single student or when I look for the second one on it does not search. I leave below code where I enter student and I am looking for students

struct Alumno
{
    char apellido[50];
    char nombre[50];
    int dni;
    int legajo;
};

int Ingresar_Alumno (int &e)
{
    FILE *cho;
    Alumno vectoralumno[TOTAL];

    if (cho=fopen("cho.dat", "wb+"))
    {
            cout << "ingrese el nombre del alumno: ";
            cin >> vectoralumno[e].nombre;

            cout << "ingrese el apellido del alumno: ";
            cin >> vectoralumno[e].apellido;

            cout << "ingrese legajo del alumno: ";
            cin >> vectoralumno[e].legajo;

            cout << "ingrese el DNI del alumno: ";
            cin >> vectoralumno[e].dni;

        fwrite(vectoralumno,sizeof(struct Alumno),1,cho);

        }
    fclose(cho);
    e++;
}

void BuscarDNI(int dni, int &q)
{
    Alumno dchof;
    FILE *x;
    if(x=fopen("cho.dat","rb"))
{
  while ( true )
  {
    fread(&dchof,sizeof(struct Alumno),1,x);
    if( feof(x) )
      break;

    if(dni == (dchof.dni))
    {
        cout << "El alumno buscado es: " << endl;
        cout << "Nombre: " << dchof.nombre << endl;
        cout << "Apellido: " << dchof.apellido << endl;
        cout << "Legajo: " << dchof.legajo << endl;
        cout << "DNI: " << dchof.dni << endl;
        cout<< "Toque cualquier boton para volver al menu anterior" << endl;
        getch();
    }
  }
        if(dni!=dchof.dni)
        {
            cout<< "Error: DNI Incorrecto" << endl;
            cout<< "Toque cualquier boton para volver al menu anterior" << endl;
            getch();
        }
    }
    fclose(x);
}
    
asked by Salva Castro 25.05.2017 в 17:01
source

2 answers

1
int Ingresar_Alumno (int &e)
{
    FILE *cho;
    Alumno vectoralumno[TOTAL];

    if (cho=fopen("cho.dat", "wb+"))
    {
            cout << "ingrese el nombre del alumno: ";
            cin >> vectoralumno[e].nombre; // 1

            cout << "ingrese el apellido del alumno: ";
            cin >> vectoralumno[e].apellido; // 1

            cout << "ingrese legajo del alumno: ";
            cin >> vectoralumno[e].legajo; // 1

            cout << "ingrese el DNI del alumno: ";
            cin >> vectoralumno[e].dni; // 1

        fwrite(vectoralumno,sizeof(struct Alumno),1,cho); // 2

        }
    fclose(cho);
    e++;
}

You are updating the registration data e (comment 1) and yet always you are saving what is found in the first record of the array (comment 2).

You should leave the writing like this:

fwrite(&vectoralumno[e],sizeof(struct Alumno),1,cho);
//     ^            ^^^ la posicion a guardar
//     para acceder a la posicion de memoria donde se encuentra el registro e

By the way, since programs in C ++ you could avoid the redundant use of struct :

fwrite(&vectoralumno[e],sizeof(Alumno),1,cho);
    
answered by 25.05.2017 / 17:12
source
3

So the vector?

The problem is that you were not really writing what you are saving, but only the first member of the vector ...

So for you to use such a large vector, what you should do is create a single Student and add it to the end of the file.

wb + or ab +?

But the other problem is that you use wb + therefore every time you were to add a user you were going to delete the contents of the file. instead I suggest the use of ab + to add the new student to the end of the file ( if you want to create a new file every time you run the program you must set conditions to do that, for example at the beginning of the program >)

It might look like this

int Ingresar_Alumno (int &e)
{
    FILE *cho;
    //Alumno vectoralumno[TOTAL];
    Alumno fulano;
    if (cho=fopen("cho.dat", "ab+"))
    {
            cout << "ingrese el nombre del alumno: ";
            cin >> fulano.nombre;

            cout << "ingrese el apellido del alumno: ";
            cin >> fulano.apellido;

            cout << "ingrese legajo del alumno: ";
            cin >> fulano.legajo;

            cout << "ingrese el DNI del alumno: ";
            cin >> fulano.dni;

        fwrite(&fulano,sizeof(struct Alumno),1,cho);

        }
    fclose(cho);
    e++;
}

void BuscarDNI(int dni, int &q)
{
    Alumno dchof;
    FILE *x;
    if(x=fopen("cho.dat","rb"))
    {
        fseek(x,0,SEEK_SET);

        while ( fread(&dchof,sizeof(struct Alumno),1,x) )
        {

            if(dni == (dchof.dni))
            {
                Search:
                cout << "El alumno buscado es: " << endl;
                cout << "Nombre: " << dchof.nombre << endl;
                cout << "Apellido: " << dchof.apellido << endl;
                cout << "Legajo: " << dchof.legajo << endl;
                cout << "DNI: " << dchof.dni << endl;
                cout<< "Toque cualquier boton para volver al menu anterior" << endl;
                getch();
                // para que no imprima error aunque halla encontrado el usuario
                // retornas la funcion una vez encontrado
                fclose(x);
                return;

            }
        }
        // si logra salir del ciclo entonces quiere decir que no lo encontro
        cout<< "Error: DNI Incorrecto" << endl;
        cout<< "Toque cualquier boton para volver al menu anterior" << endl;
        getch();

    }
    fclose(x);
}
    
answered by 25.05.2017 в 17:23