I'm getting only 4 bytes in my buffer when I try to read the whole file (weight a lot more than 4B). But when I'm reading a .txt file, I successfully recover the whole file content.
I've searched in the fopen man, in the malloc mand and in the fread man, but I cant' understand why.
char* readFile(char* path)
{
/*VARIABLES*/
FILE *inFile;
long inFileSize;
long readSize;
char *buffer = NULL;
/*OPEN FILES*/
inFile = fopen(path,"rb");
/*ERROR HANDLING : FILES*/
if(!inFile) {
return "";
}
/*GETTING FILE SIZE*/
fseek(inFile, 0, SEEK_END);
inFileSize = ftell(inFile);
rewind(inFile);
printf("The file is %ld bytes long\n",inFileSize);
/*ALLOCATING MEMORY*/
buffer = (char*) malloc(sizeof(char) * (inFileSize + 1) );
/*READ THE CONTENT AND PUT IT IN THE BUFFER*/
readSize = fread(buffer, sizeof(char), inFileSize, inFile);
/*ERROR HANDLING : readed size != to the file size*/
if (inFileSize != readSize)
{
printf("Freeing buffer\n");
free(buffer);
buffer = NULL;
}
/*ADDING THE END STRING CODE*/
buffer[inFileSize] = '\0';
/*CLOSE THE FILE*/
fclose(inFile);
return buffer;
}
Also, when I change the image extension from .jpeg to .txt, I still get 4 bytes only.
Can you help me ?