0

So on the recover.c program, my program works to recover the first 34 images, but it segfaults on the 35th. I've determined that it has to do with the fprintf function, but it worked the first 34 times. I checked to see if the file opened correctly and it did, so I'm not sure what's wrong. I also checked the info I'm writing to it and it seems alright, starting with ff d8 ff. It just segfaults when I try to write to it. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(FILE *file, int start)
{
    fseek(file, start, SEEK_SET);
    unsigned char bytes[3];
    int i = 0;
    bytes[0] = '\0';
    size_t buffer = 3;
    while (bytes[0] == '\0')
    {
        buffer = fread(bytes, 3, 1, file);
        fseek(file, -2, SEEK_CUR);
        if ((buffer == 0 || feof(file)) && start > 10)
        {
            i -= 1;
            return i;
        }


        if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff)
        {
            bytes[0] = 0xff;
        }
        else
        {
            bytes[0] = '\0';
            bytes[1] = '\0';
            bytes[2] = '\0';
        }
        i++;
    }
    i--;
    return i;
}
int i;
FILE *out;
int main(int argc, char *argv[])
{
    
    if(argc != 2)
    {
        printf("One argument only: the name of the file");
        return 1;
    }
    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        printf("File either does not exist or doesn't work.");
        return 1;
    }
    i = count(file, 0);
    char title[8];
    out = fopen("1.jpeg", "w");
    unsigned char info[200000];
    for (int j = 2; j <= 50; j++)
    {
        int offset = 3 + count(file, i + 3);
        fseek(file, i, SEEK_SET);
        printf("test0 (i=%i  offset=%i ) \n", i, offset);

        fread(info, offset, 1, file);
        if(j == 36)
        {
            printf("\n\n0x%x 0x%x 0x%x 0x%x 0x%x\n\n\n",info[0],info[1],info[2],info[3],info[4]);
            if (out==NULL)
            {
                printf("Explains a lot");
            }
        }
        printf("test1, %i\n",offset);
        
        printf("testingtesting\n\n\n");
        int k;
        for (k = 0; k < offset; k++)
        {
                //printf("test2, %i\n",k);

                fprintf(out, "%c", info[k]);
                //printf("test3, %i\n",k);
        }
        printf("File %i read, k= %i, offset = %i, i = %i\n", j - 1, k, offset, i);
        i += offset;
        printf("  %i read, k= %i, offset = %i, i = %i\n", j - 1, k, offset, i);
      
        if (out == NULL)
        {
            printf("This is broken\n");
        }
        fclose(out);
        sprintf(title, "%d", j);
        strcat(title, ".jpeg");
        out = fopen(title, "w");
        if(j == 35)
        {
            
        }
        if (out == NULL)
        {
            printf("Failed to open file.\n");
            return 1;
        }
    }


    fclose(out);
}

You must log in to answer this question.

Browse other questions tagged .