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;
}