I am working on pset 4 but my program does not spit out any jpegs. I guess that it has something to do with the data type of the variable "buffer". How to specify an array for storing "bytes"?
#include <stdio.h>
#include <cs50.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
//check command line
if (argc != 2)
{
fprintf(stderr, "Usage: recover image\n");
return 1;
}
//remember infile
char *infile = argv[1];
//open memory card
FILE *inptr = fopen(infile, "r");
if(inptr == NULL)
{
fprintf(stderr, "File could not be opened.\n");
return 2;
}
//define tools
char filename[20];
int buffer[512];
int n = 0;
FILE *outptr = NULL;
//read block size of 1 byte for 512 times until it reaches EOF (less than 512)
while (fread(buffer, 1, 512, inptr) == 512)
{
//When find a JPEG beginning
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//close the last file (except for the 1st time) and open a new file
if ( n != 0)
{
fclose(outptr);
}
sprintf(filename, "%03i.jpeg", n);
outptr = fopen(filename, "w");
n++;
}
//write bytes only if an output file is opened
if (outptr != NULL)
{
fwrite(buffer, sizeof(buffer), 1, outptr);
}
}
fclose(inptr);
return 0;
}