0

I wrote the code that logically I think should be working, it creates 50 images, however if I try to open any of them, it says

Invalid or Unsupported Image Format 

also when I tested it with check50 here is what I get (I don't really understand what this means

:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:( recovers 000.jpg correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
:( recovers middle files correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
:( recovers last file correctly
   \ expected an exit code of 0, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 154..."
https://sandbox.cs50.net/checks/f098860e96f14dfa92f3099865b3d5aa

Here is my code, any help would be appreciated

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

int main (int argc, char *argv [])
{
    // ensure correct usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover file_name\n");
        return 1;
    }

    // remember file name
    char *card = argv[1];

    // open in file
    FILE *in_file = fopen(card, "r");
    if (in_file == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", card);
        return 2;
    }

    // initialize out file and needed variables
    FILE *out_file = NULL;
    unsigned char buffer [512];
    char file_name [7];
    int counter = 0;

    // repeat until end of the file
    while (fread(&buffer, 512, 1, in_file) == 1)
    {

        // is it a start of a new JPEG?
        if (buffer [0] == 0xff &&
            buffer [1] == 0xd8 &&
            buffer [2] == 0xff &&
            (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
                // if not the first image, then close the previous file first
                if (out_file != NULL) 
                {
                    fclose (out_file);
                } 

                // then creat a new file
                sprintf(file_name, "%03i.jpg", counter);
                fopen (file_name, "w");
                if (in_file == NULL)
                {
                    fprintf(stderr, "Could not creat %s.\n", file_name);
                    return 3;
                }
                counter++;

        }        

            // buffer does not start with jpeg header? or a new file has just been created?
        if (out_file != NULL)
        {
            // write to outfile
            fwrite(&buffer, 512, 1, out_file);
        } 
    }

    // close all files opened 
    fclose(out_file);
    fclose(in_file);

  // success  
  return 0;  
}
1
  • hi I think I have a similar problem.. what did you do?
    – Ak500
    May 21, 2020 at 13:32

1 Answer 1

3

I found the answer with a very stupid mistake, and that is I open the file without assigning it to out_file

Feels bad as am frustrated all night :(

4
  • It's great you found the mistake yourself. Did you know that you can accept your own answer?
    – Blauelf
    Mar 8, 2017 at 8:52
  • Yaa I know but am waiting for the time limit Mar 8, 2017 at 12:31
  • Wasn't aware of any time limit, but it makes some sense.
    – Blauelf
    Mar 8, 2017 at 12:37
  • Thank you!! I had the same issue. Brilliant! Jun 8, 2018 at 19:24

You must log in to answer this question.

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