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:
/* 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 = newchar [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;
}