0

My code compiles and runs and the images load and seem fine BUT it skips the first image and ends at 48. There IS an image named 000.jpg that is output but that image under the name 000.jpg from what I understand is actually the image for 001.jpg and so on... (the images are shifted) a weird file (sometimes named P@) is output instead does anyone know what might be the reason?
here is my code:

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


#define BLOCKSIZE 512


int inError();



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

    // Check for invalid usage
    if (argc < 2)
    {
        return inError();
    }

    char* input = argv[1];
    if (input == NULL)
    {
        return inError();
    }

    char filename[8]; // we know it's 8 from: ###.jpg'\0'

    // open, check and read from file
    FILE *infile = fopen(argv[1], "r");
    if (infile == NULL)
    {
        printf("The file %s cannot be opened for reading\n", filename);
        return 1;
    }
// create output files
    FILE *outfile = NULL;
    typedef uint8_t BYTE;
    BYTE buffer[BLOCKSIZE];

    int jpgCount = 0;

    bool isjpghead = false;
    bool foundjpg = false;
    outfile = fopen(filename,"w");

    while (fread(buffer, sizeof(BYTE), BLOCKSIZE, infile))
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) //jpg headers begin with this
        {
            foundjpg = true;
            //if it's the jpg header mark boolean as true
            if (isjpghead == false)
            {
                isjpghead = true;
            }

            else
            {
               fclose(outfile);
            }

            outfile = fopen(filename,"w");
            sprintf(filename, "%03i.jpg", jpgCount);

            //increment jpgcount
            jpgCount ++;

            //write to output file
            fwrite(buffer, sizeof(buffer), 1, outfile);

        }
        else
        {
            if (foundjpg)
            {
                fwrite(buffer, sizeof(buffer), 1, outfile);
            }
        }


    }

//close open files to avoid memory leaks

    fclose(outfile);
    fclose(infile);
    return 0;
}



//output error
int inError()
{
    printf("Usage: ./recover image\n");
        return 1;
}
0

"Ready, Fire, Aim"

Look at this code and think carefully about what it does and in what order:

        outfile = fopen(filename,"w");
        sprintf(filename, "%03i.jpg", jpgCount);

It opens the output file in the first line, and then builds the filename to be used to open the file. That means that the first file created and opened will have a garbage file name, not 000.jpg. Maybe the order of these two lines should be reversed?

If you do a file listing in the directory, ls -al, you'll probably find a bunch of files with weird names, depending on how many times you've run the program. You should delete them.

Also, you have a stray fopen before the while loop that's going to result in a valgrind error.

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

2
  • YESS! Thank you very very much! – Sarah Nov 3 '20 at 21:19
  • Its always so much fun to see that light bulb turn on and burn bright. ;-) – Cliff B Nov 5 '20 at 8:05

You must log in to answer this question.

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