0

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

//number of bytes in jpeg file const int BLOCK_SIZE = 512;

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

//ensure proper use
if(argc != 2)
{
    printf("Usage: ./recover [IMAGE]\n");
    return 1;
}

char* infile = argv[1];
char* outfile = argv[1];


//open input file
FILE *input = fopen(infile, "r");
if(input==NULL)
{
    printf("Unable to open\n");
    return 1;
}

//open output file
FILE *output = fopen(outfile, "w");
if(output==NULL)
{
    fclose(input);
    printf("Could not create\n");
    return 1;
}

typedef uint8_t BYTE;
BYTE header[BLOCK_SIZE];
fread(header, sizeof(BLOCK_SIZE), 1, input);
fwrite(header, sizeof(BLOCK_SIZE), 1, output);

//buffer file
int16_t buffer[BLOCK_SIZE];
 while(fread(&buffer, BLOCK_SIZE, 1, input)==BLOCK_SIZE)
{
    char *filecount = argv[1];
    FILE *img= fopen(argv[1], "w");
     //start of jpeg file
if(buffer[0] == 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3] & 0xf0) ==0xe0)
{

    sprintf(filecount, "%03i.jpg", 2);
    fwrite(&buffer, BLOCK_SIZE, 1, img);
    continue;
    //check new block if start of next jpeg
    if(buffer[0] == 0xff && buffer[1]== 0xd8 && buffer[2]== 0xff && (buffer[3] & 0xf0) ==0xe0)
    {
        fclose(output);
        fclose(img);
    }
    else
    {
        fwrite(&buffer, BLOCK_SIZE, 1, img);
    }
}

}

}

1 Answer 1

1

A mistake is here FILE *output = fopen(argv[2], "w");. There is no argv[2]. Previously the code checks that argc equals 2 (and presumably passes else another message would display). The 2 arguments are indexes 0 and 1. index 0 (argv[0]) is the program name (./recover) and index 1 (argv[1]) is the raw file name as input by the user.

You must log in to answer this question.

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