the recover program I've implemented runs and compiles but does not produce any images.To debug the program I put a few "printf"s in different parts of it and realized it never passes "first of jpeg" condition(it never prints "here").I also printed the first byte of the buffer and no 0xff or 255 was printed.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <cs50.h>
int main(int argc , char* argv[] )
{
if ( argc != 2 )
{
fprintf( stderr , "Usage: ./recover memoryCard\n") ;
return 1 ;
}
//assigning memory card name to a variable
char* memory =argv[1] ;
//opening the memory card
FILE* card_raw = fopen( memory , "r") ;
if( card_raw == NULL )
{
fclose(card_raw) ;
fprintf( stderr , "Can not open memory card\n") ;
return 2 ;
}
//reading the file into the buffer
int buffer[512] ;
//found is False when no jpg is found
bool found = false ;
//counter is the number of jpgs found
int counter = 0 ;
//img is the pointer for storing images
FILE* img = NULL ;
//checks if the program reaches the end of the file(EOF)
while( true )
{
int check = fread( &buffer , 512 , 1, card_raw ) ;
if( check != 1 )
{
break ;
}
//printf("%d\n", buffer[0]) ;
//checks if this block has the start of the jpeg
if( buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0)== 0xe0 )
{
printf("here");
if( found == false )
{
found = true ;
}
else
{
fclose(img) ;
}
//specify the name of the picture
char* picname = malloc(8 * sizeof(char)) ;
sprintf( picname , "%03i.jpeg" , counter ) ;
//open the new image
img = fopen( picname , "a" ) ;
//write the block on the image
fwrite( &buffer , 512 , 1 , img ) ;
counter++ ;
}
else
{
if( found == false )
{
// readbytes = fread( buffer , 1 , 512 , card_raw ) ;
continue ;
}
else
{
//write the block on the image
fwrite( &buffer , 512 , 1 , img ) ;
}
}
}
//close files
fclose(card_raw);
if( img != 0 )
{
fclose(img) ;
}
}