0

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) ;
}

}
0

Here's the main problem:

//reading the file into the buffer
int buffer[512]  ;

The data type of int is 8 bytes in a 64 byte architecture. It's breaking the code. Interestingly, because of the way data is stored, the first four bytes read from the file, say 01 02 03 04, would be stored in the first integer, buffer[0], as 04030201.

A more appropriate data type is needed to hold the data, for instance, buffer[] should be declared with a data type of some kind that was a 1 byte unsigned integer type. ;-)

There may or may not be other issues (for another question), but this is the big one. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I can't thank you enough :) – Pegi Dec 24 '17 at 17:18
0

Be careful of memory leaks! Have to use free(), whenever malloc() is called :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .