I'm stuck. Use of undeclared identifiers img and filename... I dont get it. Something with the pointers? Could anybody give me some tips to get further. Thanks!
My code:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <stdint.h>
unsigned char buffer[512]; //create buffer space to save read part of 512 bytes by fread
int firstfound = 0; //set 0 for not found yet
int num_found = 0; //how many jpg found
char startcheck1[4] = {0xff, 0xd8, 0xff, 0xe0}; //posible start of jpg
char startcheck2[4] = {0xff, 0xd8, 0xff, 0xe1}; //posible start of jpg
int main()
{
FILE * inptr = fopen("card.raw", "r"); //create infile pointer
if(fread(&buffer, sizeof(char), 512, inptr) == 512) //as long as we can still read 512 bytes
{
fread(&buffer, sizeof(char), 512, inptr); // read theam 512 bytes
if (buffer[0] == startcheck1[0] && buffer[1] == startcheck1[1] && buffer[2] == startcheck1[2]
&& (buffer[3] == startcheck1[3] || buffer[3] == startcheck2[3])) // if beginning of jpg
{
fclose(img); // close current file
num_found++; //increment number found by 1
firstfound = 1; // make true
sprintf(filename, "00%d", (num_found - 1)); //prints new name to filename
FILE * img = fopen(filename, "a"); // create file pointer that points to filename and opens that file pointed to
fwrite(&buffer, sizeof(char), 512, img); // write buffer to file that file pointer points to
}
else
{
if(firstfound == 1) //if first start already found
{
fwrite(&buffer, sizeof(char), 512, img); //write buffer
}
}
}
fclose(img);
return 0;
}