0

recover.c is compiling and even producing 50 images but those images are not opening and neither am I able to download those images. Here is the code

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

typedef uint8_t BYTE;

int main(int argc, char* argv[])
{
    FILE* file = fopen("card.raw","r");
    if (file == NULL)
    {
        return 1;
    }
    BYTE buf[512];
    int flag = 1,fno = 0;
    char title[8];
    FILE* fp;
    while(fread(buf,512*sizeof(BYTE),1,file) == 1)
    {
        if(flag == 1)
        {
            if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] >= 0xe0 && buf[3] <= 0xef))
            {
                flag = 0;
                sprintf(title,"00%d.jpg",fno);
                fno++;
                fp = fopen(title,"w");
                fwrite(buf, 512*sizeof(BYTE), 1, fp);
            }
        }
        else
        {
            if (fno<10)
            {
                if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] >= 0xe0 && buf[3] <= 0xef))
                {
                    fclose(fp);
                    sprintf(title,"00%d.jpg",fno);
                    fno++;
                    fp = fopen(title,"w");
                    fwrite(buf, 512*sizeof(BYTE), 1, fp);
                }
                else
                {
                    fwrite(buf, 512*sizeof(BYTE), 1, fp);
                }
            }
            else
            {
                if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] >= 0xe0 && buf[3] <= 0xef))
                {
                    fclose(fp);
                    sprintf(title,"0%d.jpg",fno);
                    fno++;
                    fp = fopen(title,"w");
                    fwrite(buf, 512*sizeof(BYTE), 1, fp);
                }
                else
                {
                    fwrite(buf, 512*sizeof(BYTE), 1, fp);
                }
            }
        }
    }
    fclose(fp);
    fclose(file);
}
  • Your program is (at least) 2 times more complicated than need be because of the fno "switch" to accommodate different format strings. You can use %03d as the format string in all cases. An internet search for "printf leading 0" will yield tons of results. I found this one the most informative. Format strings are practically another language! While that does not answer or solve your problem directly, you will be able to remove many lines of (repeated) code and it should be easier to troubleshoot. – DinoCoderSaurus Jul 14 '16 at 1:56
0

Well, I've been poking at this one for several hours. The problem I was having was that it wouldn't pass check50. It was producing an "Unknown Error" and failing. I spent several hours looking for an error in your code, and consulted with a couple other people.

As it turned out, a bug got into check50 itself, which has now been fixed (but testing is still being done). So now, it turns out that it works fine (although @dinocodersaurus is correct). I tested with both last year's and this year's card.raw files and found no problems.

If you're still having problems with it, you should look into other issues, like editing one version and compiling another, etc.

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

| improve this answer | |

You must log in to answer this question.

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