My code seems to have trouble making the .jpg files in which to place the blocks of data.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
uint8_t buffer[512];
uint8_t jpeg1[4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t jpeg2[4] = {0xff, 0xd8, 0xff, 0xe1};
int main(int argc, char* argv[])
{
//open memory card file - fread the raw data
int picNum = 0;
char title[8];
FILE* img;
FILE* inptr = fopen("card.raw", "r");
if(inptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Wasn't able to open the cardfile.\n");
return 1;
}
do
{
fread(&buffer, 1, 512, inptr);
}
while((buffer[0] != jpeg1[0]) && (buffer[1] != jpeg1[1]) && (buffer[2] != jpeg1[2]) && ((buffer[3] != jpeg1[3]) || (buffer[3] != jpeg2[3])) && (fread(buffer, 1, 512, inptr) == 1));
while(fread(&buffer, 512, 1, inptr) == 1)
{
if((buffer[0] == jpeg1[0]) && (buffer[1] == jpeg1[1]) && (buffer[2] == jpeg1[2]) && ((buffer[3] == jpeg1[3]) || (buffer[3] == jpeg2[3])))
{
if(img != NULL)
{
fclose(img);
picNum++;
}
sprintf(title, "%3d.jpg", picNum);
img = fopen(title, "a");
}
fwrite(&buffer, 1, 512, img);
fread(&buffer, 1, 512, inptr);
}
fclose(img);
return 0;
}
Get this from GDB:
41 if((buffer[0] == jpeg1[0]) && (buffer[1] == jpeg1[1]) && (buffer[2] == jpeg1[2]) && ((buffer[3] == jpeg1[3]) || (buffer[3] == jpeg2[3])))
(gdb)
52 fwrite(&buffer, 1, 512, img);
(gdb)
Program received signal SIGSEGV, Segmentation fault.
__GI__IO_fwrite (buf=0x804a044 <buffer>, size=1, count=512, fp=0x1)
at iofwrite.c:41
41 iofwrite.c: No such file or directory.
Why isn't the jpg being made? Help on fixing this segfault please!
buffer[3] != (jpeg1[3] || jpeg2[3])
may seem logical to you, but the compiler will not understand it as you intend.jpeg1[3] || jpeg2[3]
is not a valid operation.