0

I get how to solve it. I have the pseudocode in mind on how to solve it. But i cannot understand how do I go about solving it though because I cannot address the most fundamental problem. How do I go about reading the file from the start to the very end

so far I only understood how to read the file to find the start of the jpeg. However I don't really understand how I should proceed from here to keep moving.

FILE* card = fopen("card.raw", "r");
if(card == NULL)
{
    printf("did not open card.raw");
    return 1;
}

int* buffer = malloc(sizeof(int) * 1024);
fread(buffer, 512, 1, card);
for(int c = 0; c != EOF; c++)
{
    if(c > 3)
    {
        if(buffer[c - 3] == 0xff)
        {
            if(buffer[c - 2] == 0xd8)
            {
                if(buffer[c - 1] == 0xff)
                {
                    if(buffer[c] == 0xe1)
                    {
                        printf("found one");
                    }
                }
            }
        }
    }
}

1 Answer 1

1

Your logic is wrong so far, so let's start from the beginning. You read from the file, 512 bytes every time, and you save them in buffer, which is an array of bytes, and 512 places of them. So you can check the buffer bytes by

  • buffer[0]
  • buffer[1]
  • buffer[2]
  • buffer[3]
  • ...
  • buffer[511]

The beginning of each image, is in the first 4 bytes of each chunk of 512 bytes. So you should check only buffer[0], buffer[1], buffer[2] and buffer[3] for the appropriate values. Not every 4 adjacent bytes. So the nested ifs are wrong in your code and I would advise you rewatch the tutorials and try again. I think this will help you get started.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

Sidenote

Also 512 bytes are enough for your buffer. You don't need 1024 of them.

Sidenote 2

Also it would be better to use uint8_t instead of int, to make sure that each BYTE you read is the same on every machine your code is compiled. That is that it's an unsigned int of 1 byte.

6
  • Thank you so much for answering. However I don't really understand how I should proceed from here to keep moving. Can you please help me understand that? while(fread(buffer, 512, 1, card) == 1) will this ensure I keep looping until end of file is reached? I dont understand how to make it read the next 512 bytes and then the next that is my main issue Aug 22, 2015 at 5:39
  • Yes this will ensure that you read until the end of file. You just have to open a new file whenever a beginning of image is found, and write all the bytes you read until a new beginning is found.
    – ChrisG
    Aug 22, 2015 at 5:46
  • thank you. this is enough to get me started. Aug 22, 2015 at 5:48
  • sorry my mistake. my code works Aug 22, 2015 at 5:53
  • Yes it does. You will have to check manually if you have reached the end of file, but you will do that later. For starters just try to loop around every 512 bytes, find an image and write it.
    – ChrisG
    Aug 22, 2015 at 5:55

You must log in to answer this question.

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