get position

hi guys,
I'm trying to get a word position from a binary file, I have a program that searches for the word, and finds it.

Do you know of a function to get the word position in the file?
http://www.cplusplus.com/reference/iostream/istream/tellg/ ?
Or did you want to get the line and column?
Thanks hamsterman, but what I want to do is, I search a string in the file, if the string is found then give me the position of the string.

Right now i'm using the algorithm package to find the string. I'm searching for something that I can give a pointer and it returns me the string position.
Still don't understand. How do you want that position described? Do you want a number of symbols before the string?
Also, post the algorithm you use (or at least a part of it).
It really isn't an algorithm yet, I'm still working on that, it's in Spanish most of it

This is my search method to find the array in the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void buscarSerie( std::fstream &busca_Serie_en_el_Archivo )
{
    int vnSerie = obtenerSerie( "Escriba el numero de Serie del Criminal");
    listaEntrada.open( "Delincuentes.dat", std::ios::in | std::ios::binary );
    if(!listaEntrada)
    {
        std::cerr << "No se pudo abrir archivo." << std::endl;
        std::exit(1);
    }
    busca_Serie_en_el_Archivo.seekg( (vnSerie - 1) * sizeof(Criminal));

    Criminal BuscarSerieenLista;
    busca_Serie_en_el_Archivo.read( (char *)(& BuscarSerieenLista), sizeof( Criminal ));

    if( BuscarSerieenLista.jalanSerie() != 0)
    {
        imprimirLinea( std::cout, BuscarSerieenLista);
    }
    else
    std::cerr << "El criminal # " << vnSerie << " no tiene informacion." << std::endl;
    listaEntrada.close();
}



That one works fine, it gets me the array number and then with another method it gives me the information contained in that array number

What I want to do is search for a word in the file and every single match it finds then it should return me all of the information contained where the search found matches

So my idea is:
1.- Check if the word is in the file, if it is, return true (or 0 like strcmp depends on my search method)
2.- Return the position of the word in the file (The word is in the container N) "This is were i'm having problems
3.- Return information on that container

Here is the code that will do this

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
50
51
52
53

void busqueda_de_String( std::fstream &dame_tipo_Crimen )
{
    // First we load the fstream
    std::fstream listaEntrada;
    std::ifstream::pos_type size;

    //Create a buffer to get the fstream
    char * memoria;

    // Open the fstream
    listaEntrada.open( "Delincuentes.dat", std::ios::in | std::ios::binary | std::ios::ate );

    // error control
    if(!listaEntrada)
    {
        std::cerr << "No se pudo abrir archivo." << std::endl;
        std::exit(1);
    }
    //getting the size of the file
    size = listaEntrada.tellg();
    memoria = new char [size];
    listaEntrada.seekg (0, std::ios::beg);

    //getting the file on memory and closing the file
    listaEntrada.read (memoria, size);
    listaEntrada.close();

    //Creating a char array to compare to in the search
    // Here i am creating a string just to use getline and give the user freedom to write whatever
    // he wants, for example "Armed Robbery" then that string is converted to char with the '\0';
    std::string tipo_Crimen_antes_de_Convertir;
    getline(std::cin, tipo_Crimen_antes_de_Convertir);
    char vtCrimen[20];

    // this is just to prevent that if the string is longer than 19 chars it will only take 19 to prevent program crashes
    const char *tCrimen = tipo_Crimen_antes_de_Convertir.data();
    int longitud = tipo_Crimen_antes_de_Convertir.size();
    longitud = ( longitud < 20 ? longitud : 19 );
    std::strncpy( vtCrimen, tCrimen, longitud );
    vtCrimen[ longitud ] = '\0';

    Criminal BuscarStringenLista;
    //For to search the memory (Still working on that)
    std::cout << "Buscando...";
    char* pos = std::search(memoria, memoria + size, vtCrimen, vtCrimen + strlen(vtCrimen));
    
    // This part is to search for the word in the file, i'm still working on this
    if(pos < memoria + size)
    {
            imprimirLinea( std::cout, BuscarStringenLista);
    }
}
Topic archived. No new replies allowed.