0

This is my code:

// recovers the location of .jpeg files

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

#include "bmp.h"

int main(int argc, char *argv[])
{
    // checks if it's proper used
    if (argc != 2) {
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;
    }

    // assigns card name to inraw
    char *inraw = argv[1];

    // opens memory card file while checking if it exists
    FILE *inptr = fopen(inraw, "r");
    if (inptr == NULL) {
        fprintf(stderr, "Could not open %s.\n", inraw);
        return 2;
    }

    // variable that stores .raw chunks
    BYTE buffer[512];

    // getting the size of card.raw in bytes
    fseek(inptr, 0L, SEEK_END);
    int size = ftell(inptr);
    fseek(inptr, 0L, SEEK_SET);


    // initialization:

    // space for filename
    char filename[8];
    // number of each file
    int filenumber = 0;
    // pointer to image
    FILE *img_ptr;
    // bool for is jpeg already initialized? 1-NO 2-YES
    int jpg_init = 1;

    // cycles through .raw length
    for (int i = 0; i < size / sizeof(BYTE); i++) {

        fread(&buffer, 512, 1, inptr);

        // jpeg isn't initialized
        if (jpg_init == 1) {

            // finds new jpeg
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) {

                sprintf(filename, "%.3i.jpg", filenumber);
                img_ptr = fopen(filename, "w");
                if (img_ptr == NULL) {
                    fprintf(stderr, "%s", "Could not make img \n");
                    return 2;
                }
                fwrite(buffer, 512, 1 ,img_ptr);
            }

            // doesn't find new jpeg

        }

        // jpeg is initialized
        else if (jpg_init == 0) {

            // finds new jpeg
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) {
                fclose(img_ptr);
                filenumber++;
                sprintf(filename, "%.3i.jpg", filenumber);
                img_ptr = fopen(filename, "w");
                if (img_ptr == NULL) {
                    fprintf(stderr, "Could not make img \n");
                    return 2;
                }
                fwrite(buffer, 512, 1 ,img_ptr);
            }

            // doesn't find new jpeg
            else {
                fwrite(buffer, 512, 1 ,img_ptr);
            }
        }

    return 0;

    }

    fclose(img_ptr);
    fclose(inptr);
}

The code shows no bugs when I compile it but I only get one jpeg. I've been debugging it for quite a while and need some pointers on what am I doing wrong

E: I did it!

Had A LOT of things wrong!

For example, "return 0" was badly placed. I rewrote my for-loop as well. And used cs50 library to use boolean expressions!

2
  • It looks like you fixed it. However, posting the full solution is prohibited by Harvard Honor Code. Instead, please edit your question and restore it to it's original state. Then, add an answer with an explanation of your fixes. THen, when the system let's you (it sometimes makes you wait two days) accept your own answer! That gets it out of the unanswered question pool. Answering your own question is also highly encouraged!
    – Cliff B
    Nov 30 '18 at 0:07
  • Oh man, I'm sorry. I'll do that!
    – Pexan
    Dec 2 '18 at 18:39
0

I had a lot of things wrong. This is some pointers (hehe) to what I did wrong and changed:

  • created a variable to calculate blocks of 512 bytes from the size of .raw file
  • I #include the stdbool library because using zeros and ones for true or false was getting me some errors
  • return 0 was wrongly placed. My loop was only looping once
  • the pointer I created for an image I had to set it to NULL (better grammar I guess)
  • rewrote my for loop with two ifs and two sub-ifs for each one of them

Another note is if recover.c is giving strange outputs when run (a strange file in the folder) just make another folder with a new recover.c and copy the code to it. It solved it for me.

You must log in to answer this question.

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