0

I found with Valgrind that in my code line 79 there is a segmentation fault in the fwrite function, but I can't find the reason why that is. So after 6 straight hours I thought that some help might be welcome!

#include <stdio.h>
#include "bmp.h"

// create new structure for the blocks of bytes to be read 
typedef struct
{
   BYTE buff[512];
} __attribute__((__packed__))
 BUFFER;

int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 1)
{
    printf("no command-line arguments accepted\n");
    return 1;
}

// declare some variables
BUFFER bf;
int photo = 0;
char* outfile = NULL;
FILE* outptr = NULL;

// open input file 
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", "card.raw");
    return 2;
}

// read until EOF
while (feof(inptr) == 0)
{
    // read the first block of 512 bytes
    fread(&bf, sizeof(bf), 1, inptr);

    // if first 4 bytes signal JPEG than proceed 
    if(bf.buff[0] == 0xff && bf.buff[1] == 0xd8 && bf.buff[2] == 0xff && (bf.buff[3] == 0xe0 || bf.buff[3] == 0xe1))
    {
        // close last image
        if (photo > 0)
        {
            fclose(outptr);
        }

        // create output file whith correct name
        sprintf(outfile, "%03i.jpg", photo);

        // open new output file
        FILE* outptr = fopen(outfile, "a");
        if (outptr == NULL)
        {
            fclose(inptr);
            fprintf(stderr, "Could not create %s.\n", outfile);
            return 3;
        }

        // write block in output file
        fwrite(&bf, sizeof(bf), 1, outptr);

        // increase photo counter by 1 for next naming
        photo++;
    }
    else
    {

        // THIS IS LINE 79!
        // write block in output file
        fwrite(&bf, sizeof(bf), 1, outptr);
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// end program
return 0;

}

1

The logic of your code is this:

  • search for a signature block
  • If found, open a new output file and write the block, or if not found write out the current block to the current open file.

The problem is that last part. In the beginning, before the first signature block is found, you haven't yet opened an output file - outptr doesn't yet exist, but you try to write to it anyways in line 79. Instant seg fault. Maybe you should test to see if the file is open before writing to it?

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

4
  • Thanks Cliff, finally learned what "Segmentation Fault" really is! Unfortunately, I got a new error in my sprintf function. This refers to outfile (a char * declared earlier as NULL). When declared char* as NULL I got no errors while compiling the program, but have a segmentation fault while running. What I read about char* NULL is that it marks the end of a string, but I'm not quite sure here.. Nevertheless when I want to declare a constant size towards char* (7) for instance, I got a "incompatible pointer types to parameter" error while compiling. Could you help me once again? I'm so close! – Jelle Postma Nov 2 '15 at 20:56
  • When you do something like char* outfile = NULL; you have declared a pointer, but it points to nothing. It also hasn't allocated any space. You've figured out that you need 7 chars, so you're trying char* outfile[7]; or at least, that's my guess. But that doesn't allocate a char array of 7 characters, it allocates an array of 7 pointers to chars. So, when all is said and done, you want a variable or array to hold a 7 character string. Your next move is this: char outfile[7]; (continues....) – Cliff B Nov 2 '15 at 21:27
  • You're almost there, but you still have a problem. This doesn't allow for the end of string marker, \0 (which, by the way, should not be confused with NULL). So, you need to add one more element to the char array. The final solution is this: char outfile[8];. You can initialize it, but if you're using sprintf() to populate outfile before you use it, there is no need to initialize. – Cliff B Nov 2 '15 at 21:34
  • Cliff, you saved me again! After I fixed this I got one last bug, which was quickly resolved. When I declared my opening output file: FILE * outptr for the second time (first time is at top of file), I had to remove the "FILE*", I think this is because I declared a new pointer the second time? Nevertheless I got 16 beautiful images --> let's go to pset5! – Jelle Postma Nov 3 '15 at 21:20

You must log in to answer this question.

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