0

include <stdio.h>

include <stdlib.h>

typedef uint8_t byte;

define SI 512

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

FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
    fclose(card);
    printf("Unable to open a file\n");
    return 1;
}


byte buffer[SI];
char title[8];

int counter = 0;
FILE* pic = NULL;

//when card can be read
while(fread(buffer,sizeof(byte),SI,card) == SI)
{
    
   
    //check for jpg
    if((buffer[0] == 0xFF && buffer[1] == 0xd8 && buffer[2] == 0xff) && buffer[3] >= 0xe0 && buffer[3] <= 0xef)        {
        
        if(open == 1)
        {
            open = 0;
            fclose(pic);
        }
        
        sprintf(title,"%03d.jpg",counter);
        //open a first picture file to write into
        pic = fopen(title, "a");
        //check if the file exist
        if(pic == NULL)
        {
            printf("This file doesnt exist\n");
            fclose(pic);
            return 2;
        }
        //write the beginning of the first file into pic file
       
         open = 1;
         counter++;
         //write the rest of bytes
          
          
    }
    //if file is open write in bytes Blocks
    if(open == 1)
    {
        fwrite(buffer,sizeof(byte),SI,pic);
    }
        
     
    
       
}
fclose(pic);
fclose(card);
return 0;

}

  • Remember, the fact that you are copy-pasting code isn't a good thing. You should use loops instead. – Abehssera Yohan Aug 27 '16 at 20:40
0

First, you aren't checking the jpeg signature fully. The fourth byte could be any value between 0xe0' and0xef`, not only the two values you are checking.

Then,your code only writes the first 512 bytes because you are not handling the possibility of multiple buffers into one single file.

In fact, I can see the comment //write the beginning of the first file ..., but I don't see any //write the next 512 bytes into file ... or so comment.

| improve this answer | |
  • I have tried quite a few times to print another 512 bytes but could never get anything else printed apart from the first 512 bytes. Just running out of ideas hehe. Is there anything that you could suggest ? I understand that after I get the jpeg started I have to write it with 512 bytes at each time until this jpeg is finished but how to do it is something I couldn't figure out yet hehe. I have extended my signatures at least now:) Thanks for that advise. – kamilucha123 Aug 27 '16 at 21:55
  • @kamilucha123 You could read and write until you find another jpeg file signature, and then rewind for 512 bytes, then continue. I think you should be able to make it with that. Good luck :) – Abehssera Yohan Aug 27 '16 at 22:04
  • Don't you think there is a better way of testing the signature's fourth byte? There's a bit of a copy pasting there. Hint : "<" and ">" ;) – Abehssera Yohan Aug 27 '16 at 22:06
  • Hehe I am feeling so useless at the moment with this. Thank again I will give it my best bash and hopefully will come out with a solution by tomorrow. I kind of understand what you are saying just do not know how to implement it just yet:) Will do my best to solve this puzzle of mine hehe. And will try to get it looking nicer too with bitwise operators somehow :) Really new to programming but loving it . Just getting my brain back on track after years is not that easy I guess haha. Thanks – kamilucha123 Aug 27 '16 at 22:13
  • I have managed to make the program work correctly and display all of the photos correctly by just adding an int with value 0 and 1 and checking whether picture is open (ready for writing):D Still need to figure out stuff with the bitwise operators tho :D – kamilucha123 Aug 27 '16 at 22:59

You must log in to answer this question.

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