0

I have written this code. The code seems to work fine but the images are not correct they are distorted. I have attached those photos below. Can you please help me and tell me where I went wrong in the code?

Check50/Test: https://submit.cs50.io/check50/13219be40309abad1fba018524d1679d65bd0e43

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // Check if user typed two arguments only.
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    /**
     * Open the file that user entered.
     * Check if the file was opened.
     */

    FILE *userFile = fopen(argv[1], "r");

    // Check if the file did open.
    if (userFile == NULL)
    {
        printf("ERROR: File couldn't be opened!\n");
        return 1;
    }

    /**
     * Read data from file in while loop.
     * If it is JPEG start storing that data on
     * another file until you find another JPEG.
     * If end-of-file reached exit the loop and
     * End the program if not continue writing.
     */

    bool Filend = false, newFound = false;   // Loop will iterate until these conditions become true.
    bool loopBreaker = false;                // Breaks from all the loops.
    bool isOpen = false;                     // Checks whether the image is already open.
    int t = 0, u = 0;                        // t- Tens, u- Unit
    char newName[8];                         // File name which will change for each image.
    BYTE Block[512];                         // Block of 512 bytes.

    // Reading the first block of memory from file.
    fread(Block, 512, 1, userFile);

    while (!Filend)
    {
        // Check if the given block is JPEG.
        if (Block[0] == 0xff && Block[1] == 0xd8 && Block[2] == 0xff && (Block[3] & 0xf0) == 0xe0)
        {
            // Get new name.
            sprintf(newName, "0%i%i.jpg", t, u);

            // Increasing value for next JPEG.
            u++;

            if (u > 9)              // u = 10 after u++?
            {
                u = 0;              // set u => 0
                t++;                // t = 1 i.e. 010 as tu
            }

            /**
             * Write data until another JPEG is found.
             */
            do
            {
                if (isOpen)     // Check if the file is already open.
                {
                    FILE *Append = fopen(newName, "a");  // Open same file.
                    fwrite(Block, 1, 512, Append);       // Append Data.
                }
                else            // Create new file to write the JPEG.
                {
                    FILE *Write = fopen(newName, "w");  // Open new file.
                    fwrite(Block, 1, 512, Write);       // Write Data.
                }

                // Check if the userFile reached end-of-file.
                if (feof(userFile))
                {
                    loopBreaker = true;
                    break;
                }

                // Go to the next block of memory.
                fread(Block, 512, 1, userFile);

                // Check if it is JPEG.
                if (Block[0] == 0xff && Block[1] == 0xd8 && Block[2] == 0xff && (Block[3] & 0xf0) == 0xe0)
                {
                    newFound = true;        // Breaks from the loop.
                    isOpen = false;         // If JPEG found get a new file.
                }
                else
                {
                    isOpen = true;          // If no JPEG found then write on same file.
                }
            }
            while (!newFound);
        }
        else        // Go to next Block of memory if it is not jpeg;
        {
            fread(Block, 512, 1, userFile);
        }

        // Check if the file reached eof.
        if (loopBreaker)
        {
            Filend = true;
        }

        // Resetting value.
        newFound = false;
    }

    return 0;
}

Result/Images:

enter image description hereenter image description here

1

I forgot! I was so focused on writing code about files, bytes and other stuff that I forgot to close files.

I added: line-75 | fclose(Append), line-81 | fclose(Write) and line-123 | fclose(userFile).

if (isOpen)     // Check if the file is already open.
{
    FILE *Append = fopen(newName, "a");  // Open same file.
    fwrite(Block, 1, 512, Append);       // Append Data.
    fclose(Append);                       // Closing file.
}
else            // Create new file to write the JPEG.
{
    FILE *Write = fopen(newName, "w");  // Open new file.
    fwrite(Block, 1, 512, Write);       // Write Data.
    fclose(Write);                      // Closing file.
}

and

    // Closing userTyped file
    fclose(userFile);

    return 0;
}

EDIT: This code is at line-84

// Check if the userFile reached end-of-file.
if (feof(userFile))
{
    loopBreaker = true;
    break;
}

Copy & cut it to line-87 after fread():

// Go to the next block of memory.
fread(Block, 512, 1, userFile);

// Check if the userFile reached end-of-file.
if (feof(userFile))
{
    loopBreaker = true;
    break;

}

You must log in to answer this question.

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