My code compiles and actually recovers 1 single image, an empty one, then it quits. I suspect the issue is related to the amount of bytes I'm reading from card.raw, but I can't be sure. I tried moving things around a lot, always get the Seg Fault. This is the only configuration that I was able to compile and run without errors, but I only get the lonely 000.jpg in the directory. Anyone can tell me what I am doing wrong? I used copy.c as a base for my code. And I left the "sanity checks" for the opening / closing of files out, here, to save space.

        // remember filenames
        char* infile = "card.raw";
        char title[8]; //leave room for \0
        uint8_t buffer[512];

        // open input file 
        FILE* inptr = fopen(infile, "r");

        // create output file for new JPG file
        FILE* outptr = 0;

        // loop through card.raw 
        for (int i = 0; i < *infile; i++)
        // number of recorded files tracker
        int counter = 0;

          if (fread(&buffer, 512, 1, inptr) == 1)
            if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] == 0xe0) || (buffer[3] == 0xe1))) 
              // create new title for new JPG file
              sprintf(title, "%.3d.jpg", counter);

              // make new JPG file
              outptr = fopen(title, "a");

              // write 512 bytes 1 by 1 to new JPG file
              fwrite(&buffer, 512, 1, outptr);   

              // close new JPG file

        // close card.raw

        // that's all folks
        return 0;
  • infile is a string, it's content is the expression "card.raw". So what exactly is this loop supposed to accomplish? for (int i = 0; i < *infile; i++) Nov 11, 2016 at 17:58
  • Yuri, maybe you should make that an answer instead of a comment?
    – Cliff B
    Nov 11, 2016 at 20:34
  • @CliffB I thought it was a simple observation and wouldn't qualify as an answer, but I'll do it then! Nov 12, 2016 at 0:44

1 Answer 1


infile is a string, it's content is the expression "card.raw".

So what exactly is this loop supposed to accomplish?

for (int i = 0; i < *infile; i++)

  • Thanks for the reply. I see. I was trying to loop through all the bytes in the card. Isn't that what I should be doing?
    – Lele
    Nov 14, 2016 at 16:31
  • Yes, but you'll have to come up with another way to read the card until EOF, because presently what your loop is doing is this: it goes from zero to the ASCII value of the first character of "card.raw", which is 99 ('c'). Nov 14, 2016 at 17:34
  • 1
    OMG I DID IT! I changed the for loop to check for feof(). Then I used the fread() inside an if () as Zamyla explained. I had to shift around counter++, fwrite() and fclose() for the output file, but I recovered all 50 photos and they all open correctly. Only now check50 gives me an error. I guess I have to ask a new question for that issue, right? Thank you so much for your input. I still have work to do, but I'm getting there :))
    – Lele
    Nov 17, 2016 at 14:57
  • Parabéns! :) yes, I think it'd be better if you asked another question, for the sake of forum organization. Nov 17, 2016 at 15:03

You must log in to answer this question.

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