Arrangement of a structure in binary file

1

We have a small error We create a user: usuarios[0].nombre and usuarios[0].pass This is saved in a binary file and all created users are displayed.

Then I create a user: usuarios[1].nombre and usuarios[1].pass This is saved in a binary file and all created users are displayed.

Then I create a user: usuarios[2].nombre and usuarios[2].pass This is saved in a binary file and only [0] and [2] are displayed.

It is followed up to the n user and only the [0] and the [n]

are still shown
#include<iostream>
#include<fstream>
using namespace std;

struct estructura{
    char nombre[100];
    char pass[100];
}usuarios[100];

struct cantidad{
    int cant;
}can;

int leer_cantidad(){
    int aux;
    ifstream file;
    file.open("cantidad", ios::in | ios::binary);
    file.read(reinterpret_cast<char*>(&can), sizeof(cantidad));
    aux = can.cant;
    file.close();
    return aux;
}

int main(){
    ofstream loco;
    loco.open("base", ios::out | ios::binary | ios::app);
    cout<<"Usuario: "<<leer_cantidad()<<endl;
    cout<<"------"<<endl;
    cin>>usuarios[leer_cantidad()].nombre;
    cin>>usuarios[leer_cantidad()].pass;
    cout<<"------"<<endl;
    loco.write(reinterpret_cast<char*>(&usuarios), sizeof(estructura));
    loco.close();

    ofstream archivaso;
    archivaso.open("cantidad", ios::out | ios::binary);
    can.cant++;
    archivaso.write(reinterpret_cast<char*>(&can), sizeof(cantidad));
    archivaso.close();

    ifstream filo;
    filo.open("base", ios::in | ios::binary);
    cout<<"Usuario: "<<leer_cantidad()<<endl;
    filo.read(reinterpret_cast<char*>(&usuarios), sizeof(estructura));
    for(int x=0; x<leer_cantidad(); x++){
        cout<<usuarios[x].nombre<<endl;
        cout<<usuarios[x].pass<<endl;
    }
    filo.close();
    return 0;
}
    
asked by José La Rosa 20.11.2016 в 00:17
source

1 answer

1

The solution proposed for this case is taken and adapted.

I hope it can help.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

void imprimir(const string, const string);

int main()
{
    char nombre[30];
    char pass[30];

    ofstream ficheroSalida("datos", ios::out | ios::app | ios::binary);

    if(!ficheroSalida)
    {
        cerr << "No se pudo abrir el archivo" << endl;
        exit(1);
    }

    cout << "Digite nombre y pass del usuario." << endl;
    cout << "> ";
    while(cin >> nombre >> pass)
    {
        ficheroSalida << nombre << ' ' << pass << endl;
        cout << "> ";       
    }

    ifstream ficheroEntrada("datos", ios::in | ios::binary);
    if(!ficheroEntrada)
    {
        cerr << "No se pudo abrir el archivo" << endl;
        exit(1);
    }

    cout << left << setw(10) << "Nombre" << setw(10) << "Pass" << endl << fixed << showpoint;

    while(ficheroEntrada >> nombre >> pass)
    {
        imprimir(nombre, pass);
    }
    ficheroEntrada.close();

    system("PAUSE");
    return 0;
}

void imprimir(const string nombre, const string pass)
{
     cout << left << setw(10) << nombre << setw(10) << pass << setw(10) << endl;
}
    
answered by 26.11.2016 в 04:42