Searching problem

Hi guys,
I'm trying to search a word in a binary file, what I did is to read the file and load it to a memory pointer, but there seems to be something wrong with my for declaration, since it doesn't search

Then if I add the size of the memory (or buffer) it gives me an error since the char array is a pointer

So I tried this, but as I expected it searches nothing. Here is the code:

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
/* strncmp example */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <cstdlib>

int main ()
{
    // 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 );

    // 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
    int tamanio = 20;
    char str2[tamanio];
    std::cin >> str2;
    int n;


    //For to search the memory
    puts ("Looking for Word...");
    for (n=0 ; n<size ; n++)
        if (strncmp (memoria,str2,10) == 0) //If i put memoria[size] it gives me error, wrong conversion from const char * to char
        {
        std::cout << "found " << memoria << "\n";
        }
  return 0;
}
size = listaEntrada.tellg(); //I think that size == 0
1
2
3
for (n=0 ; n<size ; n++)
   // if (strncmp (memoria,str2,10) == 0) 
    if (strncmp (memoria+n, str2, 10)==0)
pointer arithmetic.

If i put memoria[size] it gives me error, wrong conversion from const char * to char
memoria is a pointer, treat it like an array. So memoria[size] will be a cell (and over bounds by the way)
Last edited on
thanks ne555, I tried the method but it didn't work.

I found a new problem, strcmp will compare the entire file memory to my string, so I'm doing this entirely wrong.

I think you need to open the file with the file pointer at the end (are):
 
listaEntrada.open( "Delincuentes.dat", std::ios::in | std::ios::binary| std::ios::ate );
Also you might want to consider using std::search() from <algorithm>: http://www.cplusplus.com/reference/algorithm/search/
1
2
3
4
5
6
7
8
#include <algorithm>
#include <cstring>

char* pos = std::search(memoria, memoria + size, str2, str2 + strlen(str2));
if(pos < memoria + size)
{
    // found ...
}
Last edited on
Galik, has anyone told you you're the best?

Thanks a lot man, this totally fixed my problem, i didn't knew this package existed.


Best regards, thanks thanks thanks
Topic archived. No new replies allowed.