0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
if(argc != 2)
{
    fprintf(stderr, "please give the correct input.");
    return 1;
}


 //return 2 when unable to open image
FILE *file = fopen(argv[1], "r");

if(file == NULL)
{
    return 2;
}

unsigned char buffer[512];

int flag = 0, n = 0;

FILE *img = NULL;
char filename[8];

//get size of file
int size = 0;
fseek(file, 0L, SEEK_END);
size = ftell(file);
rewind(file);


for(int i = 0; i < size; i = i + 512) //iterating through the file
{

    if(fread(buffer, 512, 1, file) != 512) //reading and storing jpgs in buffer
    {
        //end has reached
         fclose(img);
         fclose(file);

         return 0;
    }

    //checks start of jpeg
    if(buffer[i] == 0xff && buffer[i + 1] == 0xd8 && buffer[i + 2] == 0xff && (buffer[i + 3] & 0xf0) == 0xe0)
    {
        ++flag;

       if(flag == 1) // if jpeg not already opened
       {
           //creates and opens jpeg file

           sprintf(filename, "%03i.jpg", n);
           img = fopen(filename, "w");
       }

       //if already opened
       else if(flag >1)
       {
           fclose(img);
           flag = 0;
           ++n;
       }
    }

    fwrite(buffer, 512, 1, img); //writing jpgs in buffer to img

}


}
9
  • Which line is generating the seg fault? I could probably find it easily enough, but its important that you have this skill and at least try to figure out the cause.
    – Cliff B
    Jun 17 '18 at 7:09
  • Have you isolated which line is causing the seg fault?
    – Cliff B
    Jun 19 '18 at 7:50
  • @CliffB i used help50 and valgrind and got this : ==3220== Invalid read of size 4 ==3220== at 0x5E278D4: fclose@@GLIBC_2.2.5 (iofclose.c:54) ==3220== by 0x42099A: main (recover.c:42) Looks like you're trying to access 4 bytes of memory that isn't yours? Did you try to index into an array beyond its bounds? Take a closer look at line 42 of recover.c.
    – user21692
    Jun 21 '18 at 5:48
  • @CliffB so its apparently line 42 which is causing the error..i have to still figure out why
    – user21692
    Jun 21 '18 at 5:50
  • @CliffB apparently if you close a file that was never opened it gives u a segmentation fault error. so i did this : if(img != NULL) { fclose(img); }.
    – user21692
    Jun 21 '18 at 6:18
0

Here are some clues.

Have you figured out how many times the loop has run?

After the first read has executed, line 42 tries to close the output file img. Was that file opened already or is the pointer still set to NULL?

Oh yeah, when the read executes (successfully), based on the statement, should fread() return 512 or 1 or something else?

There are numerous issues that follow, but this should get you thinking and get you going again. You really need to get comfortable using debug50 and other tools like carefully placed printf statements to see what's happening inside the program! Both would have shown a lot of these issues as well as those to come. Also, there's no simple way around it. Every programmer needs to develop good debugging skills to resolve issues like these and more!

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

You must log in to answer this question.

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