I am trying to read a file "file.raw" and 4 bytes at a time to an array and check if it has the particular 4 byte signature I am looking for. I am having trouble with this. The value of result I get is 0, instead of 4 when using fread.
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef uint8_t BYTE;
int main(void)
{
size_t result;
FILE *inptr = fopen("file.raw","r+");
//Check if file can be opened.
if (inptr == NULL)
{
printf("File Open Error\n");
return -1;
}
long int x = 0;
while(!feof(inptr))
{
// Make space for reading in to an array
BYTE *array = (BYTE *) malloc(10);
if(array == NULL)
{
printf("Array Initialization Error\n");
return -1;
}
result = fread(array,1,4,inptr);
//Exit if file not read. ** This is where I can't get past.
if(result != 4)
{
printf("File Read Error\n");
printf("%d\n",result);
free(array);
fclose(inptr);
return -1;
}
//Compare strings
if(memcmp(array,"0xffd8ffe0",4)==0)
{
printf("File Start found\n");
printf("Exiting...\n");
printf("%p\n",inptr);
free(array);
fclose(inptr);
return 0;
}
x++;
free(array);
}
printf("%p\n",inptr);
printf("%ld\n",x);
fclose(inptr);
return 0;
}