0

Scratching my head on this one folks.

My current program reads the first two chunks of 512 bytes of the first jpg (thats actually 3 reads total as the first 512 chunk is empty), but then fread fails and feof() is true! This is for the card.raw file that is over 13,000 KB, yet fread() wont read more than 1536 bytes...

The pertinent parts of my code are as follows:

int main(int argc, char* argv[])
{
    // Array to hold a 512 byte chunk of data.
    char chunk[512] = {0};
    FILE* dst = NULL;

    // open input file 
    FILE* src = fopen("card.raw", "r");
    if (src == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    while (1) {            
        // Read in the next data chunk
        size_t dataCount = fread(chunk, 512, 1, src);
        // Check the data was read ok.
        if (dataCount != 1) // THIS IS TRUE ON THE 4 CYCLE THRU THE WHILE LOOP
            break;
        /// OTHER CODE HERE TO DO REST OF WORK...
    }
return 0;
}

I placed a test for if (feof(src)) before the test of dataCount and it is true on the 4th read... Why is this thinking it is at the end of card.raw? Have I done something incorrectly?

2
  • 1
    I don't know why you're using a char array to read the data (since the data you're reading are not actually represented as chars). Yes a char is a byte long, but it'd be better if you used a uint8_t array instead. A regular char can't take a value more than 127 by the way. Anyway, I just tried to gdb your code and I found no issues at all. Here's a screenshot.
    – kzidane
    Jul 10 '14 at 23:02
  • @Kareem thanks for checking it out. I did have uint8_t but changed after checking the shorts as they seemed to use it there but tbh ive not noticed much difference so far. I made the program print out the second 512 chunk (i.e. the first valid one) and it seems to end 7 bytes after the 1024th byte position finishes when hex viewing the card.raw even though they start at the same position of 512..
    – Toby
    Jul 11 '14 at 8:57
2

I ran your program with a couple of debug lines added and it actually worked fine, reading in all the bytes of card.raw. Here's my version:

int main(int argc, char* argv[])
{
    // Array to hold a 512 byte chunk of data.
    char chunk[512] = {0};
    // FILE* dst = NULL;  //commented out as it's not used

    // open input file 
    FILE* src = fopen("card.raw", "r");
    if (src == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    int readAChunk = 0;  //keep track
    while (1) {            
        // Read in the next data chunk
        size_t dataCount = fread(chunk, 512, 1, src);
        // Check the data was read ok.
        if (dataCount != 1) // THIS IS TRUE ON THE 4 CYCLE THRU THE WHILE LOOP
            break;
        /// OTHER CODE HERE TO DO REST OF WORK...
        readAChunk++;  //increment
    }
    printf("Read %d chunks of card.raw \n", readAChunk);

    return 0;
}

When I ran this, I got read 27989 chunks of card.raw

27898*512 = 14330368 bytes which is the exact size of card.raw, so it is reading the entire card.

If you are only getting 4 reads before it stops, the problem lies elsewhere, in the code that you aren't showing.

But, it's probably because you are declaring your array as a char array. But your raw file is not made up of signed chars so you shouldn't be trying to store them in that format. You should use uint8_t instead. Be sure to #include <stdint.h> as well.

2
  • I have also noticed that when I 'p sizeof(inptr)' whilst using GDB, it gives me a value of 4. I am not sure why this is.
    – user1939
    Aug 6 '14 at 8:15
  • inptr is a pointer. pointers in 32 bit systems are 4 bytes long.
    – curiouskiwi
    Aug 6 '14 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .