I've looked at other forums on the question but it seems every solution varies based on how someone has written their code. So here's mine. Can someone show me the fault in my program.

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

int main(int argc, char* argv[])

// open image file
FILE* file = fopen("card.raw", "r");

//buffer to store images in
unsigned char data [512];

//signs indicating an image
uint8_t  FirstSign [4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t  SecondSign [4] = {0xff, 0xd8, 0xff, 0xe1};

//keep track of number of images
int counter = 001;

FILE* img;
int n;
int h;

char title[7];

//check for end of file 
while (fread(&data,512,1,file) == 1)

    n = memcmp (FirstSign, data, sizeof(FirstSign));
    h = memcmp (SecondSign, data, sizeof(FirstSign));

    //find out if image is a JPEG
    if (n == 0 || h == 0)
        //create file to write to
        img = fopen(title,"a");

        //keep track of the number of images

        //write images to current directory until a new image arrives
            fwrite(&data, sizeof(data), 1, img);
            n = memcmp (FirstSign, data, sizeof(FirstSign));
            h = memcmp (SecondSign, data, sizeof(FirstSign));

        }while(n != 0 && h != 0);

        // close file once finshed
    //if image is not a JPEG, close file



  • setting counter to 001 won't get it to display with the same format in title.

  • you did not calculate the size of title correctly. although this is probably not causing problems with your current code, it's very likely that it'll cause problems with the more correct code.

  • once you find yourself copying and pasting a piece of code, know that there's mostly a better way to do that.

  • what happens in the last iteration of the do..while loop? what is the last thing that is read? is it part of a JPEG? where does it go?

You must log in to answer this question.

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