Can somebody please help me with the code, I couldnt figure out why the pictures cannot open. It's didn't show any error. Thanks.
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *memorycard = fopen("card.raw","r");
if( memorycard == NULL)
{
fprintf( stderr, "Cound not open forensic memorycard.");
return 2;
}
//
unsigned char readblock[512];
FILE* picture;
picture = NULL;
char filename[8];
int filecount;
filecount = 0;
int jpgfound;
jpgfound = 0;
while ( fread( readblock, 512, 1, memorycard) != 0)
{
//JPEG found
if( readblock [0] == 0xff && readblock[1] == 0xd8 && readblock[2] == 0xff && (readblock[3] & 0xf0) == 0xe0)
{
if (jpgfound > 0 )
{
fclose(picture);
}
sprintf( filename, "%03i.jpg", filecount);
picture = fopen("filename", "w");
if( picture == NULL)
{
fprintf(stderr, "Can't open file.\n");
}
printf( "%s", filename);
filecount++;
fwrite( &readblock, 512, 1, picture); //&
jpgfound = 1;
}
}
fclose(memorycard);
return 0;
}
{}
button or pressCtrl
+K
to indent the lines. Any block of lines that all start with at least four spaces is considered a code block. That's why the main function is interpreted as such, but your first and last lines aren't. Select the whole code and click the button, so your whole code looks nice.