0

I know their is defiantly something wrong and missing to my code but i'm stuck and can't figure out what is missing.

It complies and when I run it does not do anything visible.

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

int main(int argc, char *argv[])
{
    // Insure proper usage of comand line
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    // Open memory card
    FILE *infile = fopen(argv[1], "r");

    // Declare outfile
    FILE *outfile = malloc(sizeof(FILE));

    // Create JPEG counter
    int jpegfound = 0;

    // Create buffer/temporary storage
    int buffer[512];

    // Create a storage for the file name
    char *filename = malloc(sizeof(char));

    // Iterate until end of memory card
    while (fread(buffer, 512, 1, infile) != 1)
    {

        // Check if it is a JPEG
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // Check if its the first JPEG
            if (jpegfound == 0)
            {
                // Name the JPEG file name
                sprintf(filename, "%03i.jpeg", jpegfound);

                // Create outfile
                outfile = fopen(filename, "w");

                //Write first jpeg
                fwrite(buffer, 512, 1, outfile);

                // Increase counter
                jpegfound++;

                // Close last file
                fclose(outfile);
            }

            // If not the first
            else
            {
                // Name the JPEG file name
                sprintf(filename, "%03i.jpeg", jpegfound);

                // Create outfile
                outfile = fopen(filename, "w");

                //Write first jpeg
                fwrite(buffer, 512, 1, outfile);

                // Increase counter
                jpegfound++;

                // Close last file
                fclose(outfile);
            }
        }
    }
    fclose(infile);
}
1

We have a couple more problems in the code. First the following lines of code:

int buffer[512];

A variable of type int is 4 bytes long on most 32-bit operating systems. If you declared your buffer to be an array of 512 ints, this means that the size of your array is 2048 bytes (4 blocks) rather than 512 bytes (a block). So you were reading 4 blocks at a time rather than only one block.

A variable of type uint8_t is 1 byte long (the 8 in the type name means 8 bits — a byte). The size of an array of 512 uint8_t is 512 bytes (a block).

On the other hand we have the following statement:

FILE *outfile = malloc(sizeof(FILE));

In the case at hand, FILE is nothing more than a pointer to a structure used internally by the input / output library to manage access to a file, its size in bytes is clearly insufficient for a jpg image of several megabytes , but most importantly, it makes no sense to reserve this memory. The pointer to a file will be managed by the fwrite () functions, which will handle the entire process, therefore it is sufficient to do:

FILE *infile = NULL;

Also:

while (fread(buffer, 512, 1, infile) != 1)

Should be:

while (fread(buffer, 512, 1, infile) == 1)
3
  • So I corrected those mistakes and now i'm getting a seg fault. the first issue was converted to this uint8_t buffer[512]; What else do you suggest modifying ? – Christopher Holder Feb 8 '20 at 23:59
  • Also tried adding typedef uint8_t BYTE; and changing it line in question to BYTE buffer[512]; but still getting a seg fault. I really don't know how to find where its coming from – Christopher Holder Feb 9 '20 at 0:11
  • I used debugger to find the memory issue and solved it. Still not getting the write output but that is a different issue – Christopher Holder Feb 9 '20 at 0:34

You must log in to answer this question.

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