0

When I finished pset4, I submitted it but received a 0 on recover. Before submitting, it was working correctly: I saw all 16 pictures (Maybe I accidentally changed something just before submitting..)

But now, I receive 16 images, with thumbnails, but when opened they give the error: Error interpreting JPEG image file (Not a JPG file: starts with 0x6a 0x70)

So I tried to find what's wrong with my code, but I can't quite seem to find the error. Any help would be appreciated.

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


int main(int argc, char* argv[])
{
FILE* file = fopen("card.raw", "r");
FILE* currentjpg = NULL;

if (file == NULL)
{
printf("Unable to open cardfile.\n");
return 1;
}

unsigned char buffer[512];  
char name[sizeof(int)];
int number = 0;

//if there is something to read
while(fread(buffer, 512, 1, file) > 0)
{

    //check for .jpg signatures
    if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) && (buffer[3] == 0xe0|| buffer[3] == 0xe1))
    { 

        //if we found a new jpg, close the old one
        if (currentjpg != NULL)
        {

            fclose(currentjpg);
            }

            //create new .jpg     
            sprintf(name,"%03i.jpg", number);
            number++;
            //and open it
            currentjpg = fopen(name, "w");
        }

        //keep writing the .jpg file, if we have found one
        if (currentjpg !=NULL) 
            fwrite(buffer, 512, 1, currentjpg);           
}
fclose(currentjpg);
fclose(file);
return 0; 
}

Thanks in advance.

1

The results you are getting are a side-effect of the actual problem. What's happening is that the code is causing a memory overwrite of data that you don't want to change.

The problem lies in these two lines:

char name[sizeof(int)];
sprintf(name,"%03i.jpg", number);

The sprintf() causes 8 chars to be written, including the end of string marker \0, but sizeof(int) = 4. Do you see a problem yet?

The problem is that the code is overwriting past the end of name by 4 characters. Those 4 chars happen to be the first 4 chars of your buffer. 0x6a 0x70 0x67 0x00, the first 4 bytes of each new file that the code is creating happen to translate to jpg\0, the 4 bytes of overwritten data from the filename. This happens because you're creating a new filename and overwriting only when the signature is detected.

If this answers your question, please click the check mark and accept. Let's keep up on forum maintenance. ;-)

0

Your best clue is the feedback you get when you are trying to open the jpg file. "(Not a JPG file: starts with 0x6a 0x70)". You know that a jpg has a header that starts with 0xff 0xd8.... And you know that you detected those headers, because you got output. So, did you or where did you write the header lines? It should be a short stroll with gdb (or even old school pencil and paper) to find the problem.

Your comment "(Maybe I accidently changed somehing just before submitting.." might compel you to recompile (make) your program to ensure you are running the version you are looking at.

You must log in to answer this question.

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