0

I have been trying to solve RECOVER from PSET4 and I keep getting a segmentation fault. Any ideas on why this happens?

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
   if (argc == 2)
   {
       //1. set files and the variables 
       FILE *input = fopen(argv[1], "r"); //input, location
       //const char *out[512];
       char pic[8];//name of the jpeg, array of 8 characters
       FILE *output = NULL ;  //where to write that does not yet exist
       BYTE buffer[512]; //array of buffers of 512 bytes to explore information 
       int jpegcounter = 0; //count how many jpegs we find

       //error if it doesn't open
       if (argv[1] == NULL)
       {
           printf("Cannot open file \n");
           return 1;
       }
       //2. create loop to open JPEGs
       else 
       {
           while (fread(buffer, sizeof(buffer), 1, input)==1)
           {
           //fread(buffer, sizeof(buffer),1,input);
           //FILE *output = fopen(pic, "w");
           if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0)== 0xe0) //first 4 bytes of 512 belonging to a buffer
           {
               if (jpegcounter == 0)
               {
                   sprintf(pic, "%03i.jpg", jpegcounter); //name it
                   jpegcounter = jpegcounter + 1; //add one image
                   
                   fopen(pic, "w");  //where to write
                   fwrite(&buffer, sizeof(buffer), 1, output);  //save it
                   
                   printf("%i", jpegcounter); //validate
               }

               else if (jpegcounter > 0)
               {
                   fclose(output);
                   
                   sprintf(pic, "%03i.jpg", jpegcounter); //name it
                   jpegcounter = jpegcounter + 1;
                   
                   fopen(pic, "w");  //where to write
                   fwrite(&buffer, sizeof(buffer), 1, output);  //save it
                   printf("%i", jpegcounter);  //validate
               }
           }
           fclose(output);
           fclose(input);
           }
       }
       
       return 0;
   }

   //if argument is not exactly 1 line
   else if (argc != 2)
   {
       printf("Usage: ./recover image \n");
       return 1;
   }
}

  • As far as I can see both calls to fwrite() functions are writing to output. And output is a null pointer? – AlStorm Prime Jun 25 at 20:38
  • I think what is happening rather than that is that input becomes null, so like fopen is returning a NULL input. But I do not know why this is happenning. – Paula Jun 25 at 20:45
  • at least in the posted code output File pointer is declared and intialized with NULL value at the beginning of main(). And I can't see this changing farther in the code. There is a line of code //FILE *output = fopen(pic, "w"); but it is commented out... Also, right before the fwrite() functions you create new file with the fopen() function. fopen() function returns a File pointer which can be farther used in fwrite() function. But in this code return value of fopen() is geting discarded and is not assigned to any varriable. – AlStorm Prime Jun 25 at 21:02
  • There are examples and description on how to use fwrite() here -tutorialspoint.com/c_standard_library/c_function_fwrite.htm Also, you can see with the input variable in your code, wich is also a File pointer. input variable was assigned value returned from fopen() function right in the beggining of main() – AlStorm Prime Jun 25 at 21:16
0

Three major problems, maybe more.

First, the current seg fault happens because the code is working through the first pass of the main while loop. Since the first 512 byte block is not a signature block, it skips past the bulk of the loop down to the following lines:

       fclose(output);
       fclose(input);
       }

Note that these two lines are inside the while loop. They should follow the loop. The fclose(output); line will cause a seg fault because it was never opened.

Next, the output files may be opened, but they're never assigned to a file pointer. Review the usage of fopen to fix this.

Finally, all the files are 512 bytes long (once these errors are fixed). Did you forget an important block of code???? ;-)

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

| improve this answer | |

You must log in to answer this question.

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