0

I've been working on recover, and it compiles correctly, and followed the pseudocode presented in the instructional video. I feel like I am probably overlooking part of the process. I think I am misconceptualising how to write to the output file, however there is probably more wrong my code is as follows:

  #include <stdio.h>

 #include <stdlib.h>

 #include <string.h>

 int main(int argc, char* argv[])
 {
 if (argc != 2)
{
    printf("Usage: ./copy infile outfile\n");
    return 1;
}

 char* infile = argv[1];


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

int *buffer = malloc(512);

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


fread(buffer, 512, 1, inptr);
{
if  (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
    char filename[100];
    sprintf(filename, "%03i.jpg", 2);
    FILE *img = fopen(filename, "w");
    fwrite(&buffer, 1, 512, img);


    if (feof(inptr))
    {
        fclose(inptr);
    }
}
}
 }
1

You are only reading 512 bytes of the file once. You need to create a loop and read the bytes into the buffer. As you do that the file pointer 'inptr' will point to the next unread byte after each iteration.

1
  • I added a forloop for the fread functiom but it still doesn't have any output. FILE* inptr = fopen(infile, "r"); int j = sizeof(inptr); int *buffer = malloc(512); if (inptr == NULL) { printf("Could not open %s.\n", infile); return 2; } for(int i = 0; i<j; i++) { fread(buffer, 512, 1, inptr); }
    – adjcode
    May 24 '17 at 17:27

You must log in to answer this question.

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