I am working on pset4's recover. My variable, outptr, is not being recognized by other parts of my do-while loop. Why is this?
#include <stdio.h>
#include <stdlib.h>
#include "jpeg.h"
int main(int argc, char* argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image");
return 1;
}
//Remember filenames
char *infile = argv[1];
// Open file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s. \n", infile);
return 2;
}
//Declaring constants and variables
int eof = 0;
BYTE buffer[512];
int counter = -1;
char image[4];
int IMAGENO = 0;
do
{
//Reading into array
fread(&buffer, 512, 1, inptr);
//Checking if EOF has been reached
if (fread(&buffer, 512, 1, inptr) == 0)
eof++;
sprintf(image, "%03i.jpg", IMAGENO);
FILE *outptr = fopen(image, "w");
//Checking if JPEG header
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
counter++;
if (counter == 0)
{
/*Start first JPEG*/
sprintf(image, "%03i.jpg", IMAGENO);
IMAGENO++;
FILE *outptr = fopen(image, "w");
fwrite(inptr, 512, 1, outptr);
}
else
{
fclose(outptr); /* Old JPEG*/
/*Start next JPEG*/
sprintf(image, "%03i.jpg", IMAGENO);
IMAGENO++;
FILE *outptr = fopen(image, "w");
fwrite(inptr, 512, 1, outptr);
}
}
else
{
if (counter > -1)
{
/*Bytes belong to currently opened JPEG*/
fwrite(inptr, 512, 1, outptr);
}
//else
//{
/* Discard 512 bytes and start loop again*/;
//}
}
}
while (eof == 0);
fclose(outptr);
fclose(inptr);
}