0

So, after some manipulations with my code I`ve got to Segmentation fault. Here is my code so far

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

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: copy infile outfile\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }
    // defining the amount of 512B patterns in input file
    int patternNum = sizeof(inptr) / 512;

    // creating a buffer for jpeg parts storage
    unsigned char *buffer = malloc(512);

    int jpgcount = 0;
    int pattern = 0;
    char *filename = malloc(sizeof ("###.jpg"));
    int jpegstream = 0;
    // checking all the file for the jpegs until its end
    while (fread(buffer,512,1,inptr) == 1)
    {
    fread(buffer,512,1,inptr);
    int a = fread(&buffer,512,1,inptr);
    return a;
    if (buffer[0] == 0xff &&
    buffer[1] == 0xd8 &&
    buffer[2] == 0xff &&
    (buffer[3] & 0xf0) == 0xe0)
    {
        sprintf(filename, "%03i.jpg", jpgcount);
        jpgcount ++;
        jpegstream = 0;
        FILE *img = fopen(filename, "w");
        fwrite(&buffer, 512, 1, img);
        fseek(inptr, pattern * 512, SEEK_SET);
        pattern ++;
        fclose(img);
        jpegstream ++;
    }
    else if (jpgcount > 0)
            {
                FILE *img = fopen(filename, "w");
                fseek(img, jpegstream * 512, SEEK_SET);
                fwrite(&buffer, 512, 1, img);
                fseek(inptr, pattern * 512, SEEK_SET);
                pattern ++;
                jpegstream ++;
                fclose(img);
            }
    else
    {
        fseek(inptr, pattern * 512, SEEK_SET);
        pattern ++;

    }

    }
    fclose(inptr);
    free(buffer);
    free(filename);
    return 0;
}

The interestiong thig is, that segmentation fault appeared when I`ve changed end of card method from do {bunch of code} while(condition) to while(condition) {bunch of code}/].

Before that, it created 49 jpeg files, named correctly, which had different sizes (I've checked through "ls -l"commad in terminal), so I assume that determination of jpegs starts and ends worked at lieast somehow. But all the jpegfiles were blank and contained only zeroes. So I`ve messed up with recording to jpegs somewhere.

And last but not least, when I used do {bunch of code} while(condition) checking, 049.jpg size was 0 and program continued to search for jpegs in file without stopping. So these are the problems I`ve stuck in so far. Would be very gratefull if someone could give me some hints or advises how to get through this.

0

You should definitely not use &buffer. It's the memory address of the buffer variable, not the memory address of the allocated memory. In case of an array, like unsigned char buffer[512];, those are identical, but not in your case of a pointer variable. fread(&buffer, ... would overwrite that variable and whatever comes next, and the next time you use fread(buffer or fwrite(buffer, it will dereference a pointer with garbage value inside, likely leading to a segmentation fault (if overwriting buffer and other variables did not already crash the programme).

Do not use more than the fread in the loop condition, you seem to have more and ignore the previous blocks read.

sizeof(inptr) is 4 or 8 (32bit or 64bit code, respectively), as inptr is a pointer, storing a memory address. It is not the file size (which could be obtained in various ways, e.g. fseek(inptr, 0, SEEK_END); int filesize = ftell(inptr); fseek(inptr, 0, SEEK_SET);), it's not even necessary if you check the return value of fread.

I have a vague idea what you use ftell and fseek for, and why you need to fclose(img). You could do that later if img was declared outside the loop. While it is inefficient, opening/closing a file once per 512 byte block might work if you use "w" for the first block and "a" for the others (opening a file in "w" would erase the file content if the file already exists).

1
  • Thank you very much for your help! Your advises were super functional. I've corrected the use of pointers, threw away all the unnecessary variables and code constructions, corrected the condition loops, put declaration of some variables (like img) out of the loops and it worked!!=) I don`t know if it is correct to post the working code here, but if it is, I would be glad to share=) Thank you once more! Jan 5 '20 at 15:40

You must log in to answer this question.

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