0

I have been getting segmentation fault on my code, when i try to debug it, it gives me this error:

Error opening file Could not open file: /build/buildd/eglibc-2.19/libio/iofread.c The file could not be found on the file system.

I have searched quite extensively and tried everything that i can come up with but i couldn't solve my problem. Here is how my code looks like:

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

typedef uint8_t  BYTE;

int StartOfJpg(BYTE*);

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

    // if a jpg file is currently being written, isWriting is equal to 1
    char fileName[7];
    int fileCount = 0;
    int isWriting = 0;
    FILE* outptr = NULL;
    while(1)
    {
        // allocate 512 bytes of memory and read into it
        BYTE *block = malloc(sizeof(BYTE) * 512);
        if(fread(block, sizeof(BYTE), 512, inptr) != 512)
        {
            // if 512 byte cannot be read, free the memory, close the file
            /// and exit the loop
            free(block);
            if(isWriting)
                fclose(outptr);

            break;
        }

        // if first 4 bytes signifies a start of JPG
        if(StartOfJpg(block))
        {
            // if a jpg is already being written, close it.
            if(isWriting)
            {
                fclose(outptr);
            }

            // create a new jpg file and increase the fileCount.
            isWriting = 1;
            sprintf(fileName, "%03i.jpg", fileCount);
            outptr = fopen(fileName, "w");
            fileCount++;

        }

        // if a jpg file can be written, write.
        if(isWriting)
        {
            fwrite(block, sizeof(BYTE), 512, outptr);
        }

        // free the dynamically allocated memory
        free(block);
    }

    // close input file
    fclose(inptr);

    return 0;
}

int StartOfJpg(BYTE* block)
{
    // if first 3 bytes are JPG's signature
    if(block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff)
    {
        // get the first 4 bits of the fourth byte.
        // shift the bytes four times so we can get the following format:
        // 0000abcd
        // in binary 14 equals to 00001110,
        // use bitwise and operator to compare the last for bits. 
        // if it starts with 1110, return 1
        BYTE fourthByte = block[3] >> 4;
        if((fourthByte & 14) == 14)
            return 1;
    }

    return 0;
}

I get the error on while's condition, in function fread. The error only occurs on the fourth iteration, after writing my first set of 512 byte to 000.jpg. When it comes to reading the second block of the first jpg, it throws an segfault error.

Any help is appreciated, thanks...

1

The problem was fileName array, it needs to be 8, not 7. sprintf was overwriting important memory because of that.

You must log in to answer this question.

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