0

My code compiles/executes and retains 50 JPEGs, however, these JPEGs are broken, I have gone through my code many of times, have discarded other versions, I seem to be missing something I can't spot anymore and frankly think I am not grasping the concept here, If someone could point out my mistakes, i.e. I get a better understanding of the concepts, where I have gone wrong, would bring me back on schedule. Sorry for the mess here, I am also not grasping the concept of adding code and text here.

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

typedef uint8_t  BYTE;
int main(void)
{

FILE* inptr = fopen("card.raw","r");
if(inptr == NULL){

    fprintf(stderr,"Could not open %s.\n", "card.raw");
    return EXIT_FAILURE;
}
BYTE buffer[512];
FILE* jpeg_p = NULL;
int JPEG_number = 0;
char filename[] = ###.jpg;

//start reading into 512 bytes into buffer....
while(fread(&buffer,sizeof(buffer), 1 ,inptr) == 1)
{

    if(feof(inptr)){
    break;
    }
        //find beginnig of JPEG...
           if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff 
           && (buffer[3] == 0xf0 || buffer[3] == 0xe0))


        {
            //start of jpeg found?
            if(jpeg_p){
            fclose(jpeg_p);
            }


          //make a new JPEG, by pluging in Filename and adding JPEG#

             sprintf(filename,"%03i.jpg", JPEG_number);
             JPEG_number++;

            //open file and write buffer to it
            jpeg_p = fopen(filename,"w");
            fwrite(&buffer,sizeof(buffer),1,jpeg_p);


                if(NULL == jpeg_p)
                {
                //fclose(inptr);
                fprintf(stderr, "Could not create %s.\n", filename);
                EXIT_FAILURE;
                }


            }
                   //write to file if JPEG
                    if(jpeg_p){
                    fwrite(&buffer,sizeof(buffer),1,jpeg_p);
                    }
        }
                    // close infile
                    fclose(inptr);

                    // close outfile
                    fclose(jpeg_p);
  // success
 return EXIT_SUCCESS;
}

enter image description here

enter image description here

1

When the code finds a signature, it opens a new file and writes the buffer out. Then, in the code that is meant to handle the blocks of image data that follows, it writes the signature block out a second time, so the first block is duplicated in the output file.

Also, the code did not compile because of this line: char filename[] = ###.jpg; It would be simpler to just declare an array with 8 chars. This also makes me wonder if you have been saving and recompiling your code as you change and work on it.

As a side note, please spend some time with style50 and learn how to properly format and indent code. It might have been easier to see the problem if the code was properly formatted.

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

| improve this answer | |
  • First of all thank you very much for your editing of my comment, secondly, your very appreciated side note, I will indeed look into that, I was not aware of this tool. You are right style is very important, i.e. the right indentation, I will work on that. Unfortunately, I messed up a lot of my indentation upon copying and inputting it into my comment here. I have added a screenshot to show I had already made an array of chars-value of 8, when posting my comment and pasting my code, I only copied my comment note above. As you can see it is compiling and running. – Der Traumwanderer Jan 27 '18 at 7:12
  • I thought my code was duplicating the first block into the output file. – Der Traumwanderer Jan 28 '18 at 6:20

You must log in to answer this question.

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