Need some feedback on a program that recovers jpg images.

This is a discussion on Need some feedback on a program that recovers jpg images. within the C Programming forums, part of the General Programming Boards category; I've been watching the lectures from Harvards introductory programming course cs50 and working through the problem sets to learn programming. ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    Need some feedback on a program that recovers jpg images.

    I've been watching the lectures from Harvards introductory programming course cs50 and working through the problem sets to learn programming. I'm currently working on pset5 and would like some feedback on the last part of the problem set, image recovery. The goal is to write a program that recovers 50 jpgs from a "forensic image" of a Compact Flash card and name them from 001.jpg to 050.jpg. The program works but I'm not sure If I solved the problem correctly. How badly designed is this implementation ?

    Thanks.

    Code:
    /****************************************************************************  
    
    * recover.c
    
    *
    
    * Computer Scince 50
    
    * Problem Set 5
    
    *
    
    * Recovers jpg images from image card.raw
    
    ****************************************************************************/  
    
    #include <cs50.h>
    
    #include <stdint.h>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
    
    typedef uint8_t BYTE;
    
    
    
    int
    
    main(void)
    
    {
    
    
    
        // open card.raw file
    
        FILE *inptr = fopen ("card.raw", "r");
    
        if (inptr == NULL)
    
        {
    
            printf("could not open card.raw file\n");
    
            return 1;
    
        }
    
    
    
        // size of a block in FAT file system 
    
        int block_size = 512;
    
    
    
        // buffer store one block
    
        BYTE buffer[block_size];
    
    
    
        // store image filename 
    
        char image_name[8];
    
    
    
        // recovered jpgs will be named with a number starting from 1  
    
        int number = 1;
    
    
    
        // keep track of when an image is found 
    
        bool image_found = false;
    
    
    
        // pointer to current jpeg file on hard disk
    
        FILE *outptr = NULL;
    
    
    
        // look for jpgs until EOF is reached 
    
        while (feof(inptr) == false)
    
        {
    
            // read 512 btye block into buffer
    
            fread(buffer, block_size, 1, inptr);
    
    
    
            // check buffer for jpeg signature
    
            if (buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 &&
    
                (buffer[3] == 224 || buffer[3] == 225))
    
            {
    
                // if a previous jpeg was found close file 
    
                if (image_found == true)
    
                {
    
                    fclose(outptr);
    
                }
    
    
    
                image_found = true;
    
    
    
                // name new image
    
                sprintf(image_name, "%.3d.jpg", number);
    
                number++; 
    
                
    
                // create new jpg image file on hard disk
    
                outptr = fopen (image_name, "w");
    
                if (outptr == NULL)
    
                {
    
                    printf("could not create jpeg file\n");
    
                    return 2;
    
                }     
    
                
    
                // write 512 byte block to jpg file on disk
    
                fwrite(buffer, block_size, 1, outptr);        
    
            }
    
    
    
            // continue writing parts of a jpg to file on disk 
    
            else if (image_found == true)
    
            {
    
                fwrite(buffer, block_size, 1, outptr);        
    
            } 
    
        }
    
    
    
        // close card.raw 
    
        fclose(inptr);
    
    
    
        // close last jpg
    
        fclose(outptr);
    
        return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        // look for jpgs until EOF is reached 
        while (feof(inptr) == false)
        {
    Read this: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    Code:
            // read 512 btye block into buffer
            fread(buffer, block_size, 1, inptr);
    You should be checking the return value of fread to see if it worked or not.

    You also don't repeat the process 50 times, so you're only doing one conversion.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Code:
            // check buffer for jpeg signature
    
            if (buffer[0] == 255 && buffer[1] == 216 && buffer[2] == 255 &&
    
                (buffer[3] == 224 || buffer[3] == 225))
    
            {
    
                // if a previous jpeg was found close file 
    
                if (image_found == true)
    
                {
    
                    fclose(outptr);
    
                }
    
    
    // Should NOT this all be in a else section
    
    
                image_found = true;
    
    
    
                // name new image
    
                sprintf(image_name, "%.3d.jpg", number);
    
                number++; 
    
                
    
                // create new jpg image file on hard disk
    
                outptr = fopen (image_name, "w");
    
                if (outptr == NULL)
    
                {
    
                    printf("could not create jpeg file\n");
    
                    return 2;
    
                }     
    
    
     // End of Should NOT this all be in a else section        
    
    
                // write 512 byte block to jpg file on disk
    
                fwrite(buffer, block_size, 1, outptr);        
    
            }
    Added bold comments to code.

    Tim S.
    Last edited by stahta01; 07-07-2011 at 06:09 PM.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    Post

    Thanks for the help guys. I had no Idea about feof. I think that putting that block of code in an else condition wont work. I tried it just to be sure and sure enough it was only able to recover one jpg. Again thanks for all the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I use images in a c program
    By errigour in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 09:11 PM
  2. How do I use images in a c program
    By errigour in forum C Programming
    Replies: 0
    Last Post: 11-23-2010, 08:52 PM
  3. Replies: 7
    Last Post: 07-22-2010, 12:45 PM
  4. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 05:38 PM
  5. feedback, please...
    By major_small in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2003, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21