Llevo toda la tarde intentándolo hacer funcionar, pero por algún motivo el fread parece que no funciona, porque ft no tiene el inicio del texto del archivo...
He probado todo lo que se me ha ocurrido y no he conseguido nada...fread devuelve que ha leido el número de caracteres correcto, pero luego nada, no están los caracteres que deberían estar...
Pongo aquí el código que tengo hecho (es posible que esté un poco guarrete de tanto poner printf's)...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int buscar(char * ft, char * cadena, unsigned long * bytes);
unsigned long flen(FILE *f);
int cargar_fichero(FILE *f, char *contenido, unsigned long *nbytes);
int main (int argc, char* argv[]){
int i;
FILE * fp;
char * ft;
int a;
unsigned long bytes;
char data[] = {0x0D, 0x0C, 0x0D, 0x0D, 0x03, 0x0D, 0x0D, 0x04, 0x0D, 0x0D, 0x03, 0x0D, 0x0D, 0x04, 0x0D, 0x0D, 0x00};
for(i=1; i < argc; ++i){
fp
= fopen(argv
[i
], "rb"); if(fp != NULL){
if((a=cargar_fichero(fp, ft, &bytes)) == 0)
return -1;
if(buscar(ft, "footnotes.xml", &bytes) == 0)
else{
fseek ( fp
, 0 , SEEK_SET
); //Devolvemos puntero al inicio if(buscar(ft, data, &bytes) == 0)
}
return 0;
}else
printf("No se ha podido abrir el archivo %s", argv
[i
]);
}
return 0;
}
/*
Busca en el archivo la cadena indicada (imitiendo el \0)
*/
int buscar(char * ft, char * cadena, unsigned long * bytes){
if(longi < 1)
return -2;
int i=0;
int k=0;
while(i
< *bytes
){printf("Va bien con c=%x, k=%d\n", ft
[i
], k
); if(ft[i] == cadena[k]){
k++;
while(k < longi && ++i < *bytes && ft[i] == cadena[k])
k++;
if(k == longi)
return 0;
else if(i >= *bytes){
return -1;
}else{
k=0;
}
}else
++i;
}
return -1;
}
unsigned long flen(FILE *f){
unsigned long pos, len;
fseek(f
, pos
, SEEK_SET
);
return len;
}
int cargar_fichero(FILE *f, char *contenido, unsigned long *nbytes){
if((contenido
= (char*) malloc((*nbytes
= flen
(f
)) * sizeof(char))) == NULL
) return 0;
return fread(contenido
, sizeof(char) , (*nbytes
/sizeof(char)) , f
); }