0

I'm stuck pretty early on the "Recover" problem. My code doesn't find any JPEGS in the file "card.raw'

Here's the relevant bit of my code:

#include <stdio.h>
#include <stdlib.h>

// Constants
int const chunk = 512;

// Definitions
typedef unsigned char BYTE; // defines a "BYTE" as type unsigned char - an unsigned 8-bit char

// Declare Main, Open input file etc.
[Code not shown - but I'm pretty sure this part works]

// Find beginning of JPEG file

// Calculate size of input file in 512-byte chunks
fseek (inptr, 0L ,SEEK_END); // Go to end of file
int length = ftell (inptr); // Return the cursor position - which will equal the size of the file in bytes
fseek (inptr, 0L, SEEK_SET); // Go back to the beginning of the input file
printf("the file's size is %d Bytes\n", length);
int no_of_chunks = (length/chunk); // Calculates size of file in 512-byte chunks
printf("that = %d chunks\n", no_of_chunks);

// For each 512-byte chunk of the input file:
for (int i = 0; i < no_of_chunks; i++)
{
    // Declare a buffer to read each chunk of data into
    BYTE buffer[512]; // Creates an array of 512 bytes (called "buffer")

    // Read input file to buffer in 512-byte chunks
    fread(&buffer, sizeof(buffer), 1, inptr);

    // Declare a JPEG-counter variable that counts the number of JPEGS we have found
    int counter = 0;

    // Check first 4 bytes to see if it is a jpeg
    if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2]== 0xff) && ((buffer[3] & 0xf0) == 0xe0))
    {
        printf("We've found a jpeg\n");
        counter++;
        printf("Counter now = %i\n", counter);
    }
    else
    {
        printf("No jpeg found\n");
        printf("Counter now = %i\n", counter);
    }
}

What I expect to happen is that I will end up with 40 statements saying "We've found a jpeg", and counter will end up at 40 (plus a number of statements saying "No jpeg found". What I actually get is many many statements saying "No jpeg found" followed by "Counter now = 0" (presumably, 23,000 or so times!)

What am I doing wrong? Thanks!

PS - I have looked at the other questions and answers, but still can't figure it out.

1

Since counter is reset to 0 after every fread, it will never be 40. Perhaps filter out the noise by making the else condition something different, perhaps printf("."); to just "mark" a read. IMO you're making it too hard on your cognitive load to filter through 23,000+ lines of output.

3
  • Doh! Of course it is! Counter needs to be initialised outside the loop.Done that, and now counter reaches 50 (which is what it should do). So I know I've found all 50 jpegs in the "card.raw" file. Tbh I disagree on the cognitive load - all I need to do is see that the counter finishes at 50; I don't need to run through 23,000 lines of output. But I am wasting screen space! Thanks loads for your help. Feb 23 '17 at 16:49
  • If you hadn't used "all that screen space", you would have seen the "We've found a jpeg" messages, with counter = 1 and .... well we wouldn't be having this conversation :) Feb 23 '17 at 16:57
  • I see your point! Feb 23 '17 at 17:46

You must log in to answer this question.

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