Problems with my for loop and a binary file

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 );
}
My program needed the next line before returning the pointer to 0:
listaEntrada.clear();
Topic archived. No new replies allowed.