0
#include <stdio.h>
#include <stdint.h>
#include <cs50.h>

typedef uint8_t BYTE;

int main (void)
{
    char* filename = "000.jpg";
    int counter = 0;
    bool found = false;
    FILE* infile = fopen("card.raw", "r");
    if(infile == NULL) 
    {
        printf("Error in opening file");
        return(-1);
    }

    FILE* outfile = fopen(filename, "w");
    if(outfile == NULL) 
    {
        printf("Error in creating file");
        return(-1);
    }


    while (true)
    {
        BYTE* inptr = malloc(sizeof(BYTE) * 512);

        //int read_char = fread(inptr, sizeof(inptr), 1, infile);

        /*if (read_chars < 512)
        *{
        *    fseek(infile, 512 - read_chars, SEEK_CUR);
        *}
        */

        if (((inptr[0] == 0xff) && (inptr[1] == 0xd8)) && (inptr[2] == 0xff))
        {            

            if (found)
            {
                fclose(outfile);
                counter++;
                sprintf(filename, "%3d.jpg", counter);
                fopen(filename, "w");   
            }
            found = true;   
        }

        if (found)
        {
            fwrite(inptr, sizeof(inptr), 1, outfile);
        }
        free(inptr);

        if (feof(infile))
        {
            fclose(outfile);
            fclose(infile);
            break;
        }
    }
    return 0;
}

Here i am getting segmentation fault after getting the first jpg, i am not able to find out the mistake. Any help would be appreciated.

UPDATE:::: Here's my updated code, still having segmentation error. Changed BYTE* inptr to BYTE* buffer (for ease). Changed outfile to outptr (for ease). Changed filename to jpgout (for ease). Changed infile to inptr (for ease).

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

typedef uint8_t BYTE;

int main (void)
{
    char* jpgout = "000.jpg";
    int counter = 0;

    //found is used to see if first jpg reached or not
    bool found = false;

    FILE* inptr = fopen("card.raw", "r");
    if(inptr == NULL) 
    {
        printf("Error in opening file");
        return(-1);
    }

    FILE* outptr = fopen(jpgout, "w");
    if(outptr == NULL) 
    {
        printf("Error in creating file");
        return(-1);
    }

    BYTE* buffer = malloc(sizeof(BYTE) * 512);
    while (true)
    {
        fread(buffer, sizeof(buffer), 1, inptr);

        if ((((buffer[0] == 0xff) && (buffer[1] == 0xd8)) && (buffer[2] == 0xff)) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            if (found)
            {
                fclose(outptr);
                counter++;
                sprintf(jpgout, "%3d.jpg", counter);
                outptr = fopen(jpgout, "w");
            }
            found = true;   
        }

        if (found)
        {
            fwrite(buffer, sizeof(buffer), 1, outptr);
        }

        if (feof(inptr))
        {
            fclose(outptr);
            fclose(inptr);
            break;
        }
    }

    free(buffer);
    return 0;
}

2 Answers 2

1

The code posted has a number of serious issues. First and most serious, there are no fread calls, except for the one that is commented out. Without the fread, the while loop is just an infinite loop because EOF will never be reached. The seg fault is most likely being generated because it is exhausting system resources as it runs.

The code also fails to check the 4th byte of the signature. Fixing that should be simple enough.

The code could be made much more efficient by allocating inptr once, before the while loop and freeing it once after the loop. As written, it allocates and frees that memory on every pass, so instead of doing it once, it will do it about 40,000 times for a 20 meg input file - a waste of resources and processing time.

You might want to think about how you could incorporate the fread into the while statement. Also, there's no need for an fseek call in this program, since the program is doing a linear read through the input file from beginning to end. The fseek will take care of positioning the file pointer automatically.

There may be more issues, and you may cause some new ones when you recode, but this should get you going. Any new issues are for a new question. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • yeah, sorry, i had fread(***) before but then commented out and posted it, now its corrected. I'll try to put free() outside while loop and update u.:) Commented Dec 15, 2016 at 9:22
  • Still giving a segmentation error. should i post a new question? Commented Dec 15, 2016 at 9:30
1

You should be testing the first 4 bytes to check for a jpeg signature, not 3.

1
  • Thanks, :) added it, but still having segmentation fault. Commented Dec 15, 2016 at 9:38

You must log in to answer this question.

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