0

Not sure why there are not results from this program, recover literally does nothing. Better than my old program that created over 500 images I guess.

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

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    char *infile = argv[1];

    FILE *inptr = fopen(infile, "r");

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

    typedef uint8_t BYTE;

    BYTE buffer [512];
    // sets up buffer as an array with 512 elements of size BYTE

    FILE *img = NULL;

    {
        int filenumber = 0;

        if (buffer[0] == 0xff || buffer[1] == 0xd8 || buffer[2] == 0xff || (buffer[3] & 0xf0) == 0xe0 )
        {
            filenumber ++;
            char filename [8];

            if (filenumber == 1)
            {
                sprintf(filename, "%03i.jpg" , filenumber - 1);
                img = fopen(filename, "w");
                //img is a pointer
                fwrite(buffer, sizeof(BYTE),512,img);
                // buffer is usually an array, so I'm not sure why I need the adress for the buffer
                // img is a pointer, as it should be
                // writes from the buffer into the img
            }

            else
            {
                fclose(img);
                // fclose closes the file pointer, img
                sprintf(filename, "%03i.jpg" , filenumber - 1);
                img = fopen(filename, "w");
                // img is a pointer
                fwrite(buffer, sizeof(BYTE),512,img);
                // buffer is usually an array, so I'm not sure why I need the adress for the buffer
                // img is a pointer, as it should be
                // writes from the buffer into the image
            }
        }
        else if (filenumber > 0)
        {
            // write to already existing file
            fwrite(buffer,sizeof(BYTE),512,img);
            // buffer is usually an array, so I'm not sure why I need the adress for the buffer
            // img is a pointer, as it should be
            // writes from the buffer into the image
        }
    }
    while(fread(buffer, sizeof(BYTE), 512, inptr) == 512);
}
0

If you're going to use a do/while loop, there actually has to be a "do" statement somewhere.

Having said that, why are you doing a do/while instead of a while statement? (the plain while loop has the while statement before the code block.)

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

1
  • I have changed my code to have the while fread function at the top of the block. Thank you for suggestion! Still I must have other issues with my code. Is there anything else you notice?
    – michael
    Apr 21 '20 at 18:33

You must log in to answer this question.

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