0
int main() 
{    
 FILE* raw = fopen("card.raw","r"); 
    if( raw == NULL) 
    { 
        printf("ERROR OPENING\n"); 

    } 
    uint8_t buffer[512]; 
    while(fread(buffer,1,512,raw)==512) 
    {
        if(buffer[0]==0xff && buffer[1]==0xd8 && buffer[2]==0xff && (buffer[3]&0xf0)==0xe0) 
        break; 

    } 
    fseek(raw,-512,SEEK_CUR);

    char filename[13];
    int count=0;
    while(fread(buffer,1,512,raw)==512)     
        {
            sprintf(filename,"%03d.jpg",count);
            FILE* recov = fopen(filename,"w");
            if(recov == NULL)
                {
                    printf("could not create file\n");
                }

            fwrite(buffer,1,512,recov);    
            while(fread(buffer,1,512,raw))
            {

            if(buffer[0]==0xff && buffer[1]==0xd8 && buffer[2]==0xff && (buffer[3]&0xf0)==0xe0) 
                 break; 
            else
                fwrite(buffer,1,512,recov); 
            }   
            fseek(raw,-512,SEEK_CUR);
            count++;
            fclose(recov);
        }


}
0

First problem:
This won't work: char filename[4]; You need more than 4 bytes to hold a name like "000.jpg". Count how many chars are inside the quotes (don't forget the null terminator). Can you tell how many bytes you'll need?


Second problem:
It's not my place to teach boolean logic, so you'll have to study it by yourself, but the negation of a == b && c == d isn't a != b && c != d

The AND operator changes to OR when negated... so the correct negation would be a != b || c != d or even !(a == b && c == d)

This loop is doing that first kind of (incorrect) negation: while(buffer[0]!=0xff && buffer[1]!=0xd8 && buffer[2]!=0xff && (buffer[3]&0xf0)!=0xe0);

You should be using OR here. The negation operator would also work, as I've pointed out above.


Third problem: You'll need to check for the end of the file inside that do-while loop and exit the program after it, otherwise your last file (which will never find another jpg sig to stop the loop after it) will make your loop run forever (or until the system's file size limit).


Fourth and minor problem: You're starting your jpg names from 001.jpg, but you should be starting from 000.jpg

5
  • I have editied code. I want to ask can i store int as an char or not (to fix my filename). Nov 6 '16 at 1:40
  • Yes, you can. It's not really an int, but an ASCII char which represents the int. I mean, in the end it's all chars when you use sprintf. Nov 6 '16 at 2:14
  • I am gettiing invalid jpeg for this code. Nov 6 '16 at 2:28
  • some pixels shows up in the corner and rest is grey image Nov 6 '16 at 2:43
  • You've changed your code so much that I think it's better that you put your old code back there so my answer can help others with the same problems you were having, then accept the answer to the problems you've fixed and open another question so we can tackle the other problems. Nov 6 '16 at 12:21

You must log in to answer this question.

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