2

I'm pretty confused with recover.

I've seen in previous posts that it is wrong to use feof , but the problem is that when I use fread the images won't show. However, when I use feof I can see all the images.

It seems that when I use fread the images wont write completily (they are smaller in size), but I can't find why that is.

I hope someone can help.

enter image description here

//define memory block
#define MBLOCK 512

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


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

    //open card file

    char* sdCard= "card.raw";
    FILE* sdCardFile = fopen(sdCard, "r");

    if (sdCardFile == NULL)
    {
        fclose(sdCardFile);
        printf("Could not open %s\n", sdCard);
        return 1;
    }


    typedef uint8_t  BYTE;
    BYTE buffer[MBLOCK];
    int count = 0;
    char outptr[8];
    FILE* img;


    //repead until end of card
    while (fread(&buffer, MBLOCK, 1, sdCardFile) == 1)
    {

        //read 512 bytes into a buffer
        fread(&buffer, MBLOCK, 1, sdCardFile);


        if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            //if there is a jpeg opened, close it!
            if (count > 0)
            {
                fclose(img);
                printf("closed!");
            }

            //store that we've found a jpeg
            count++;

            //print the jpeg into the current directory
            sprintf(outptr, "%03d.jpg", count - 1);

            //now, I can open the jpeg and write 512 bytes at a time
            img = fopen(outptr, "a");
            printf("opened!");
            fwrite(&buffer, MBLOCK, 1, img);              
        }
        else
        {
            if (count > 0)
            {
                fwrite(&buffer, MBLOCK, 1, img);
                printf(".");
            }
        }


    }
    fclose(sdCardFile);
}
9
//repead until end of card
while (fread(&buffer, MBLOCK, 1, sdCardFile) == 1)
{

    //read 512 bytes into a buffer
    fread(&buffer, MBLOCK, 1, sdCardFile);

So, why do you feel that it is necessary to read a second block before processing the first one? This has the effect of throwing away every other block from the input file. The first read in the while statement isn't just a test for end of file. It does execute a read from the input file.

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

| improve this answer | |
  • Thanks a lot! It workss!! – Belu Oct 1 '15 at 15:41

You must log in to answer this question.

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