0

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 ?

Amperclock
  • 379
  • 2
  • 16
  • You can start with trapping any fseek error – Grantly Dec 07 '17 at 22:51
  • @Grantly I don't have any fseek ones :( . In fact, the variable "inFileSize" which gets it value with fseek, is correct. – Amperclock Dec 07 '17 at 22:59
  • FYI: [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Dec 07 '17 at 22:59
  • *How* do you know how many bytes you're getting? You wouldn't be treating the jpeg file as a string, would you? And FWIW, `fseek(inFile, 0, SEEK_END);` is explicitly undefined behavior per the C standard. It may work on your implementation, but it's non-portable. – Andrew Henle Dec 07 '17 at 23:00
  • @Barmar Yup, let met take it out ! Thanks, by the way. – Amperclock Dec 07 '17 at 23:00
  • @AndrewHenle I know it because I strlen the result of the function. – Amperclock Dec 07 '17 at 23:00
  • 4
    @Amperclock JPEG is a binary file, not a text string. `strlen()` returns the position of the first 0 byte, JPEG has zero bytes inside the file. – Barmar Dec 07 '17 at 23:01
  • If you want to know how much as read, print `readSize`. – Barmar Dec 07 '17 at 23:02
  • 1
    Yep, strlen is the worst thing to use on a binary character array – Grantly Dec 07 '17 at 23:02
  • Also, you have undefined behavior when you do `buffer[inFileSize]` after `buffer = NULL;`. – Barmar Dec 07 '17 at 23:02
  • @Barmar I Think I get it. So what should I use instead ? sizeof don't work (and that's normal). – Amperclock Dec 07 '17 at 23:08
  • Just use `readSize`. – Barmar Dec 07 '17 at 23:09
  • @Barmar So to access this value outside my function, I need to call the pointer of it. – Amperclock Dec 07 '17 at 23:10
  • You need to return the size from the function somehow. You could return a structure that contains the size and pointer to the array. – Barmar Dec 07 '17 at 23:12
  • @Barmar I'll return a 2D array (char** myarray). The first slot is the content, the second is the readSize. – Amperclock Dec 07 '17 at 23:19
  • Yous can use `stat` to find the size of the file. – Weather Vane Dec 07 '17 at 23:38
  • @Amperclock That's a silly way to do it. Arrays should be used for uniform data, structures should be used for heterogeneous data. – Barmar Dec 08 '17 at 00:58
  • Discussion of the issues around using `fseek` and `ftell` to get the file length can be found [here](https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file) – Barmar Dec 08 '17 at 01:05
  • regarding: `buffer = (char*) malloc(sizeof(char) * (inFileSize + 1) );` 1) the returned value from `malloc()` has type `void*` which can be assigned to any pointer. casting just clutters the code, making it more difficult to understand, debug, etc 2) the expression: `sizeof(char)` is defined in the C standard as 1. Multiplying anything by 1 has no effect and just clutters the code. 3) Always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Dec 08 '17 at 05:11

0 Answers0