0

I save the name of the picture in picname and open it in img and do the rest of the copying from memory card on img , the program compiles and runs fine but no image is saved in the current directory.It does not run what's inside the while loop at all. Here is a section of my code where the copying happens :

while( fread( buffer , 1 , 512, card_raw ) == 512 ) 
{
    //checks if this block has the start of the jpeg
    if( buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0 ) == 0xe0 )
    {
        if( found == false )
        {
            found = true ;

        }
        else
        {
            fclose(img) ;
        }

        counter++ ;
        //specify the name of the picture
        char* picname  = malloc(sizeof(char) * 7) ;
        sprintf( picname , "%03i.jpg" , counter ) ;

        //open the new image
        img = fopen( picname , "a" ) ;

        free( picname ) ;
        //write the block on the image
        fwrite( buffer , 1 , 512 , img ) ;

        readbytes = fread( buffer , 1 , 512 , card_raw ) ;

    }
    else
    {
        if( found == false )
        {
            readbytes = fread( buffer , 1 , 512 , card_raw ) ;
        }
        else
        {
            //write the block on the image
            fwrite( buffer , 1 , 512 , img ) ;
        }

    }
}
0

Haven't done an in-depth analysis, but this jumped out at me.

while( readbytes == 152 )

See anything wrong? ;-)

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

5
  • Doesn’t fread return the number of times it reads size from the file ? So as long as it’s value in this case is 512 it means it hasn’t reached the end of the file ? – Pegi Dec 4 '17 at 21:10
  • It does. But read the number that is in the while statement carefully! – Cliff B Dec 4 '17 at 22:53
  • I changed 152 to 512 :) still doesn't work – Pegi Dec 7 '17 at 10:52
  • I am trying to understand why it does not go into the while loop at all – Pegi Dec 14 '17 at 8:37
  • 1
    There are a number of errors in the code. The space allocated for the output file name doesn't allow for the end of string marker. File numbering doesn't start at 0. There are too many freads in the program, resulting in data being skipped/not processed. Some of these issues are causing other data to be corrupted before being written into the output files. It'll be a good exercise for you to track down these issues and any others that may remain. If you have any other issues, please open a new question instead of extending this one. – Cliff B Dec 14 '17 at 9:33

You must log in to answer this question.

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