0

Here is updated code with answer.

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

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

    if (argc != 2){
        fprintf(stderr, "Please supply file name\n");
        return 1;
    }

    char *infile = argv[1];
    FILE *inptr = fopen(infile, "r");

    if (inptr == NULL){
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    uint8_t buf[512];//Buffer for fread
    int fr = 1;
    char fileName[50] = "000";
    int count = 0;
    int y = 1;

    do{

        if(y == 1){

            //*******Read from file first time around***********
            fr = fread(buf, 512, 1, inptr);

        }


        //*******Check for jpg signature*********
        if(buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] & 0xf0) == 0xe0){
                //If signature found create file and write to it
                if(count < 10){
                    sprintf(fileName, "00%d.jpg", count);
                    FILE *outptr = fopen(fileName, "w+");//Create a file using filename from above
                    fwrite(buf, 512, 1, outptr);//Write to file the contents of buf
                    count++;
                    fclose(outptr);
                }else{
                    sprintf(fileName, "0%d.jpg", count);
                    FILE *outptr = fopen(fileName, "w+");//Create a file using filename from above
                    fwrite(buf, 512, 1, outptr);//Write to file the contents of buf
                    count++;
                    fclose(outptr);
                        }
                y = 1;

                FILE *outptr = fopen(fileName, "a");//open file to append more data

                    while(y == 1 && fr >= 1){
                        //*********Continue reading 512 byte blocks and writing to output file
                        fr = fread(buf, 512, 1, inptr);//fread(data, size, number, inptr)

                            if(buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] & 0xf0) == 0xe0){
                                y = 0;
                                //fseek(outptr,-512,SEEK_CUR);
                                fclose(outptr);

                    }else{//End if statement

                        fwrite(buf, sizeof(buf), 1, outptr);

                    }
                    }

            }

    } while(fr >= 1);//End while loop if fread returns less than 1

    //close infile
    fclose(inptr);

    return 0;
    }
1

The logic of the program has some serious flaws. While individual statements are functioning correctly, its the overall logic that isn't working. Go back and look at what the program is doing.

  1. Read until first signature is found.
  2. Open the first file and write out the first block.
  3. In the inner while loop, read the next block.
  4. Write the block out to the current file.
  5. Check for signature. If not found, keep reading, writing and checking.
  6. If signature found, open a new file and write the same last written block to the new file.
  7. Go back to outer loop and read next block, which will be the second block in a file.
  8. Keep reading blocks until the next signature is found and go to step 2.

Note that steps 6, 7, 8 and 2 have the effect of skipping over a file. Also, step 6 adds the first block of the next file to the end of the current file.

There's some rethinking to do here to get the logic right. The code is also probably corrupting file content, but I figure that this was digging deep enough. I also suspect a problem with processing data beyond the end of file marker. Have fun! ;-)

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

  • I updated the code after your advice though really the logic is/was sound. I only added two lines of code which basically opened the file before the second while loop and added fseek to offset the current FILE pointer. This has seemed to work though now I'm only getting 24 images but they all load now :-) I also compressed the code as it was hard for me to read top to bottom so I can't imagine how hard it was for someone else :-) Thank you for your continual help it's much appreciated!! – ndjustin20 Feb 21 '18 at 4:18
  • The two freads are still throwing away half of the signature blocks and therefore every other file. – Cliff B Feb 21 '18 at 4:28
  • Figured it out! Thank you again for the help. – ndjustin20 Feb 21 '18 at 6:40

You must log in to answer this question.

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