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.