This is my code for PSET4. Can anyone explain why it is not working? I have tried debugging forever. But I get various combinations of file limit exceeded and segmentation faults that just don't go away.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(void)
{
int pcounter = 0;
FILE* raw = fopen("card.raw", "r");
if (raw == NULL)
{
printf("Could not open card.\n");
return 2;
}
BYTE *buffer = malloc(512);
while ((fread(buffer, 512, 1, raw)) != 0)
{
char str[7];
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 1110)
sprintf(str,"%03d.jpg", pcounter);
pcounter++;
FILE* outptr = fopen(str, "w");
do
{
fwrite(buffer, 512, 1, outptr);
fread(buffer, 512, 1, raw);
}
while (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] & 0xf0) != 1110);
fclose(outptr);
}
free(buffer);
fclose(raw);
}