0

I have a segmentation fault that I can't figure out. Can you please help me? It has been 4 days and I have no idea what to fix. Thank-you

Here is my code:

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

int main(int argc, char* argv[])
{
// open the card.raw file and check for NULL/
FILE* file = fopen("card.raw", "r");
if (file == NULL) {
    printf("Could not open card.raw\n");
    return 1;
}
// outfile
FILE* out = NULL;

// buffer
char buffer[512] = {0};

// boolean for if it has completed the first jpeg
int first = 0;

// boolean for if it is the end of the file
int endoffile = 1;

// boolean for if the current 512 block is a jpeg
int jpeg = 0;

// variable for number of jpegs finished
int numberofjpegs = 0;

// name of jpeg file
char * nameoffile = "";

while (endoffile == 1) {
    // if the end of file
    if (fread(buffer, 1, 512, file) != 512) {
        fclose(out);
        endoffile = 0;
    }
    // else if it has the jpeg begining
    else if (buffer[0] == (char)0xff && buffer[1] == (char)0xd8 && buffer[2] == (char)0xff) {
        jpeg = 1;
    }
    //else if it is NOT the first jpeg
    else if (first == 1) {
        fwrite(buffer, 512, 1, out);
    }
    // if it is a new jpeg
    if (jpeg == 1) {
        // if it is NOT the first jpeg
        if (first == 1) {
            // close the old out
            fclose(out);
        }
        // get the new name
        sprintf(nameoffile, "%03d", numberofjpegs);
        // add 1 to the number of jpegs
        numberofjpegs++;
        // make the new file
        out = fopen(nameoffile, "w");
        //write the first line
        fwrite(buffer, 1, 512, out);
        // set jpeg to false
        jpeg = 0;
    }
}
return 0;

}

2
  • Change it to unsigned char buffer[512]; or BYTE buffer[512]; since both do basically the same thing. Don't set it equal to anything. Aug 2 '16 at 0:52
  • Are you sure that you have the correct line with the seg fault? GDB normally displays the next line to be executed, not the one that just ran. I'm more inclined to think that the error is with the previous line related to the output file pointer.
    – Cliff B
    Aug 2 '16 at 1:01
3

The root cause of your seg fault is this:

char * nameoffile = "";

When you create a string in memory, the space allocated is fixed at that time. In this case, you created a string of length 0 plus the end of string marker. Later, when you try to create the filename with the sprintf() call, the code tries to insert a longer string into this space, triggering the seg fault.

As for typing your buffer as char, that is not the problem. While it's a better idea to type it as uint8_t, the char type is a one byte unsigned int type, which works. I know it works because that's how I successfully implemented the buffer the first time I completed this pset. Once the string length is dealt with, yours works for that too.

There are definitely other issues, like the file names themselves, etc., but that's another question.

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

3
  • I have tried to change that line to: 'char* nameoffile = NULL;' but it still does the segmentation fault Thank-you very much!
    – Jon
    Aug 2 '16 at 0:44
  • You created a pointer to a string and set it to NULL. Did you later malloc some space for it? Why don't you try the following: char nameoffile[8];
    – Cliff B
    Aug 2 '16 at 0:56
  • Thank-you! that works very well!!!
    – Jon
    Aug 2 '16 at 1:32

You must log in to answer this question.

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