First, you need to know the object in greater depth FILE.Basic notions https://en.cppreference.com/w/cpp/io/c .Actually std::FILE is a pointer that gets a different value of knot when a file is opened by function https://en.cppreference.com/w/cpp/io/c/fopen to read and write you must use the functions https://en.cppreference.com/w/cpp/io/c/fread and https://en.cppreference.com/w/cpp/io/c/fwrite respectively https://en.cppreference.com/w/cpp/io/c/fscanf and https://en.cppreference.com/w/cpp/io/c/fprintf if you want to read in format; all these functions expect to receive a pointer std::FILE to operate on it.Read the file.I see in your code that you open a file in the reading mode (read "r") binaria (binary "b"(c):FILE * usuario = fopen("usuarios.dat", "rb");
So the read-to-use function will be std::freadwhich receives four parameters:Memory address to read.Data size to read.Number of data to read.Pointer to std::FILE to read.Therefore, to read your structure you will need:An integer (int) temporary to read numerical data.A temporary character buffer to read text data on.Assuming that before the name and the last name you have kept the length of each one of them, your reading process might look like:int i;
char c[200]{};
usuario u;
std::FILE *f = fopen("usuarios.dat", "rb");
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f);
u.id = i;
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f); // Longitud de 'nombre'
// Leemos en 'c' 'i' datos de tamaño 'char' desde 'f'.
std::fread(c, sizeof(char), i, f); // 'nombre'
u.nombre = c;
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f); // Longitud de 'apellido'
// Leemos en 'c' 'i' datos de tamaño 'char' desde 'f'.
std::fread(c, sizeof(char), i, f); // 'apellido'
u.apellido = c;
Write in the file.You must open the file in the script mode (write "w") binaria (binary "b"(by consistency with the above):FILE * usuario = fopen("usuarios.dat", "wb");
So the read-to-use function will be std::fwritewhich receives four parameters:Data management to write.Size of data to write.Amount of data to write.Pointer to std::FILE in which to write.Therefore, to write your structure you will need:An integer (int) from which numerical data will be written.Your writing process might look like:int i;
usuario u;
std::FILE *f = fopen("usuarios.dat", "wb");
// Escribimos un dato de tamaño 'int' en 'f'.
std::fwrite(&u.id, sizeof(int), 1, f);
// Escribimos un dato de tamaño 'int' en 'f'.
i = nombre.length();
std::fwrite(&i, sizeof(int), 1, f); // Longitud de 'nombre'
// Escribimos desde 'nombre.data()' 'i' datos de tamaño 'char' en 'f'.
std::fread(nombre.data(), sizeof(char), i, f); // 'nombre'
// Escribimos en 'i' un dato de tamaño 'int' desde 'f'.
i = apellido.length();
std::fwrite(&i, sizeof(int), 1, f); // Longitud de 'apellido'
// Escribimos desde 'apellido.data()' 'i' datos de tamaño 'char' en 'f'.
std::fwrite(c, sizeof(char), i, f); // 'apellido'
All together: by reference.The right thing would be for reading and writing operations to be encapsulated in their own function:void leer_dato(usuario &u, std::FILE *&f)
{
int i;
char c[200]{};
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f);
u.id = i;
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f); // Longitud de 'nombre'
// Leemos en 'c' 'i' datos de tamaño 'char' desde 'f'.
std::fread(c, sizeof(char), i, f); // 'nombre'
u.nombre = c;
// Leemos en 'i' un dato de tamaño 'int' desde 'f'.
std::fread(&i, sizeof(int), 1, f); // Longitud de 'apellido'
// Leemos en 'c' 'i' datos de tamaño 'char' desde 'f'.
std::fread(c, sizeof(char), i, f); // 'apellido'
u.apellido = c;
}
void escribir_dato(const usuario &u, std::FILE *&f)
{
int i;
// Escribimos un dato de tamaño 'int' en 'f'.
std::fwrite(&u.id, sizeof(int), 1, f);
// Escribimos un dato de tamaño 'int' en 'f'.
i = nombre.length();
std::fwrite(&i, sizeof(int), 1, f); // Longitud de 'nombre'
// Escribimos desde 'nombre.data()' 'i' datos de tamaño 'char' en 'f'.
std::fread(nombre.data(), sizeof(char), i, f); // 'nombre'
// Escribimos en 'i' un dato de tamaño 'int' desde 'f'.
i = apellido.length();
std::fwrite(&i, sizeof(int), 1, f); // Longitud de 'apellido'
// Escribimos desde 'apellido.data()' 'i' datos de tamaño 'char' en 'f'.
std::fwrite(c, sizeof(char), i, f); // 'apellido'
}
Other things to keep in mind.The object std::FILE belongs to the C bookstores, since you have labeled the question as C++, Forget it and don't use it.. Use the flows (stream) data std::ifstream and std::ofstream.