0

Currently working on the Pset4 recover.c assignment. My code compiles correctly, however, when I execute, fopen return NULL. Based on my understanding from the shorts on file pointer and the lecture, if fopen returns NULL, it implies that the computer doesn't have enough memory to store the pointer to the file that I am trying to open. When I run the function, this is the error I get

~/pset4/recover/ $ ./recover card.raw
Not enough memory to store the memory_file1

^ This implies that the error is taking place at the first instance of opening up fopen. (i.e. when I write

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

    if (memory_file == NULL)
    {
        printf("Not enough memory to store the memory_file1\n");
        return 1;
    }

The following is my code:

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



typedef uint8_t BYTE;

int main(int argc, char *argv[])

{
//This is the variable that keeps a track of the number of JPEG files that we have found so far. It increases by 1 when the computer is able to find the JPEG signature at the start of a 512 block Bytes. The increase by 1 takes place at the end of this loop.
    int x = 0;

    //check to make sure there is indeed one command line argument (i.e in addition to the the ./recover argument)
    if (argc != 2)
    {
        printf("Please provide one command line argument which is the address of address of the forensic image in the IDE directory\n");
        return 1;
    }

    //open the file up and store the file pointer in memory_file
    FILE *memory_file = fopen("argv[1]", "r");

    if (memory_file == NULL)
    {
        printf("Not enough memory to store the memory_file1\n");
        return 1;
    }

    //This is the while loop which is to go on forever unless the break condition below is met (i.e. the end of file is reached).
    while (1)
    {

        //The following code helps ensure that the cursor to the memory file is updated by 512 (i.e. to the next block of 512 BYTES) at each iteration of the while loop.
        int i = 0;
        fseek(memory_file, (512 * i), SEEK_SET);
        i++;


        //The following code checks if we have reached the end of the file. If so, we break out of the while loop and return 0 to exist from the main function. Otherwise we continue until this condition has been reached.
        if (feof(memory_file) != 0)
        {
            break;
        }

        unsigned char BYTES[512];
        fread(BYTES, 1, 512, memory_file);

        if (BYTES[0] == 0xff && BYTES[1] == 0xd8 && BYTES[2] == 0xff && (BYTES[3] & 0Xf0) == 0xe0)
        {



        //This initial condition is checking to see if we have encountered the first JPEG file. If x is indeed 0, then we have just found our first JPEG file.
        if (x == 0)
        {

            char filename[8];

            //The following code somehow changes the name of the jpg file from 000.jpg to 00x.jpg where x represents the decimal number of the image we have been able to identify so far.
            sprintf(filename, "%03i.jpg", x);

            FILE *image = fopen(filename, "a");

            if(image == NULL)
            {
            printf("Not enough memory to store the memory_file2\n");
            return 2;
            }

            //The following code is intended to take the next 512 block of BYTES from the memory_file and write it in the file pointed to by image.
            fwrite(memory_file, 1, 512, image);

            //This is the variable that keeps a track of the number of JPEG files that we have found so far. It increases by 1 only the computer is able to find the JPEG signature at the start of a 512 block Bytes. The increase by 1 takes place at the end of this loop.
            x++;

            //Closing the file for this iteration
            fclose(image);
        }

        else
        {
            char filename[8];

            //The following code somehow changes the name of the jpg file from 000.jpg to 00x.jpg where x represents the decimal number of the image we have been able to identify so far.
            sprintf(filename, "%03i.jpg", x);

            FILE *image = fopen(filename, "a");

//Question 1 - so as you can see above, I have closed the previous pointer to the first JPEG image and then used the same pointer (i.e. FILE *image) to then be used as a pointer to 2nd (and beyond) JPEG files. Is this going to result in my first JPEG file being overwritten by the 2nd JPEG file and so forth?

            if(image == NULL)
            {
                printf("Not enough memory to store the memory_file3\n");
                return 3;
            }

            //The following code is intended to take the next 512 block of BYTES from the memory_file and write it in the file pointed to by image.
            fwrite(memory_file, 1, 512, image);

            //This is the variable that keeps a track of the number of JPEG files that we have found so far. It increases by 1 only the computer is able to find the JPEG signature at the start of a 512 block Bytes. The increase by 1 takes place at the end of this loop.
            x++;

            fclose(image);
        }
    }

        //The following code is executed when the first four bytes don't give us a JPEG signature
        else
        {
            if(x != 0)
            {
            char filename[8];

            //The following code somehow changes the name of the jpg file from 000.jpg to 00x.jpg where x represents the decimal number of the image we have been able to identify so far.
                sprintf(filename, "%03i.jpg", (x - 1));

                FILE *image = fopen(filename, "a");

                if(image == NULL)
                {
                printf("Not enough memory to store the memory_file4\n");
                    return 4;
                }

            //The following code is intended to take the next 512 block of BYTES from the memory_file and write it in the file pointed to by image.
                fwrite(memory_file, 1, 512, image);
            }
        }
    }
    return 0;


}
2

This "argv[1]" is the string literal argv[1] (because of the "s), and no such file name exists.

You must log in to answer this question.

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