0

The code compiled successfully, but only made two files 000.jpeg and 001.jpeg. I also cannot open these two files. I used valgrind to check if any memory issues. It's all good. Can I read one byte at a time when looking for jpeg signature and write a block of 512 when writing to new file? I don't know why there are only two output files? when I tried to open 001.jpeg. it shows "Unknown error: File size is bigger than allowed (8MB). Size is 16342528 bytes". why does the code keep writing into one file?

Here is my code:

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

#define BLOCK_SIZE 512

typedef uint8_t BYTE;

int main (void)
{
    //open the file to be recovered
    FILE* in_file = fopen("card.raw", "r");

    //check if file can be open
    if (in_file == NULL)
    {
        printf("cannot open the file\n");
        return 1;
    }

    //one block
    BYTE buffer[BLOCK_SIZE];

    //open a new file
    FILE* image = NULL;

    //keep track of how many images have been recovered
    int counter = 0;

    //read all blocks until end of file
    while(fread(&buffer, sizeof(BYTE) * BLOCK_SIZE, 1, in_file) == 1)
    {
        //iterate through each block
        for (int i = 0; i < BLOCK_SIZE; i++)
        {
            fread(&buffer[i], sizeof(BYTE), 1, in_file);
        }

        //if find a jpeg image
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && 
            (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            //make sure start writing in a new file
            if (image != NULL)
            {
                fclose(image);
            }

            char title[3];

            //write a new file
            sprintf(title, "%03d.jpg", counter);
            counter++;

            image = fopen(title, "a");

            //make sure open a file successfully
            if (image == NULL)
            {
                printf("Could not write the file.\n");
                return 2;
            }

            //write a block
            fwrite(&buffer, sizeof(BYTE) * BLOCK_SIZE, 1, image);
        }

        //write in existing file
        else if (image != NULL)
        {
            fwrite(&buffer, sizeof(BYTE) * BLOCK_SIZE, 1, image);
        }
    }

    //close input file
    fclose(in_file);

    return 0;
}
3
  • Please add a description of what is or is not happening. What command did you use to run the file? What was the output? What have you done to diagnose the problem? What do you think is the problem? It is against forum policy to simply post code with no explanation as was done here. – Cliff B Nov 8 '16 at 22:26
  • sorry about that. first time use the forum. – Xuying Shao Nov 9 '16 at 0:20
  • hello, I changed my code a bit and it actually made two output, which cannot be open. I checked there is no memory issue. And I think the logic is right. kinda confused. – Xuying Shao Nov 9 '16 at 0:50
0

First question: what is the string length (including the end of string marker) of the name of a file?

            char title[3];

Once that's fixed, the next problem is how the end of file condition for the input file is handled. The code currently reads a block of data from the input file, processes it, and then checks to see if end of file has been detected. Because of this, an extra block of data will be added to the last file.

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

3
  • I changed the while condition. But it only recovers 24 images this time, and they are garbage. I changed the code above. – Xuying Shao Nov 9 '16 at 20:04
  • Yo did change the while condition, but you didn't remove the fread that follows, so you have two consecutive freads. This means that you're throwing away a lot of data, along with half of the signatures. – Cliff B Nov 9 '16 at 20:11
  • So the while condition reads input files and does comparison at the same time. Also, I find that in sprintf function, I also need to give space to ".jpg", so it should be char title[8], right? I finally got it work correctly. Thank you very much. – Xuying Shao Nov 9 '16 at 21:00

You must log in to answer this question.

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