I guys, i have a problem with binary files
A file is opened in the beginning of the main then a case to give the user a choice:
1.- Show the content of the file
2.- Search for a name
3.- Search for a type of crime
4.- Search for a serial number
5.- Close
At first I had every search method open the file and close it, but that on the long run is not as efficient as using the memory buffer. So I tried using the memory buffer with a for loop, but when I try to run the program again the file either is closed or can't be searched again.
Here is an example of a search part
void busqueda_de_Nombre( )
{
Criminal primerLista;
//The next lines are for getting the size of the file
listaEntrada.read( (char*)(& primerLista), sizeof(Criminal));
listaEntrada.seekg( 0, std::ios::end );
int tamanio = listaEntrada.tellg();
listaEntrada.seekg( 0, std::ios::beg );
// Then with the size of the files I get the number of classes my file has
Criminal criminales;
int objetos = tamanio / sizeof(criminales);
std::string buscando_nombre;
std::cout << "Ingrese Nombre: ";
getline(std::cin, buscando_nombre);
int entero_comparador;
int contador;
int n;
for ( n = 0; n < objetos; n++ )
{
//A string is compared to the contents in the file, if there is a match
//it gives me the contents of that Class instance
listaEntrada.read((char*)(& primerLista), sizeof(Criminal));
if( (entero_comparador = buscando_nombre.compare(primerLista.jalaNombre() ) ) == 0 )
{
break;
}
else
{
contador++;
}
}
listaEntrada.seekg(contador * sizeof(Criminal));
listaEntrada.read((char*)(& primerLista), sizeof(Criminal));
if(!entero_comparador)
{
imprimirLinea(std::cout, primerLista);
std::cout << std::endl;
}
else
std::cerr << "El tipo de crimen " << buscando_nombre << " no existe o esta mal escrito." << std::endl;
//In theory, this one returns the pointer to the beginning so I
//can search again, but when I try to make a new search NOTHING happens
listaEntrada.seekg( 0, std::ios::beg );
}