file io - Solving Error in `./recover': free(): invalid next size (normal): 0x0000000001ddd270 *** Aborted in c -
code must read 512 bytes @ time file has raw jpegs in order retrieve them. when jpeg found, following blocks of 512 bytes stored in a file until new jpeg found, once new jpeg found, file img closed , opened new store new image. code runs fine if comment or erase fclose(img); line, othwerwise shows error above.
here's code
#include <stdio.h> #include <cs50.h> #include <stdlib.h> int main (int argc, char *argv[]){ if(argc != 2){ fprintf(stderr, "usage: ./recovery image\n"); return 1; } file *card = fopen(argv[1], "r"); if (card == null){ fprintf(stderr, "cannot open image\n"); return 2; } file *buffer1 = malloc(sizeof(card)); int n = 0; int picsnum = 0; while(fread(buffer1, 512, 1, card)){ n ++; } //printf("%i", n); bool foundjpeg = false; int j = 0; bool fstjpeg = false; file *img = null; int counter = 0; fseek(card, 0, seek_set); for(int = 0; < n; i++){ unsigned char fst; unsigned char snd; unsigned char thrd; unsigned char fth; unsigned char buffer2[508]; fread(&fst, 1, 1, card); fread(&snd, 1, 1, card); fread(&thrd, 1, 1, card); fread(&fth, 1, 1, card); fread(buffer2, 508, 1, card); //printf("%i,%i,%i,%i,", fst, snd, thrd, fth); if(fst == 0xff && snd == 0xd8 && thrd == 0xff && (fth & 0xf0) == 0xe0){ printf("found\n"); if(fstjpeg){ fclose(img); } printf("%i,%i,%i,%i,", fst, snd, thrd, fth); foundjpeg = true; picsnum++; fstjpeg = true; char jpeg[8]; sprintf(jpeg, "%03i.jpg", j); j++; img = fopen(jpeg, "w"); } else { foundjpeg = false; } if(fstjpeg){ fwrite(&fst, 1, 1, card); fwrite(&snd, 1, 1, card); fwrite(&thrd, 1, 1, card); fwrite(&fth, 1, 1, card); fwrite(buffer2, 508, 1, card); counter++; printf("%i,", counter); } } fclose(card); printf("%i,\n", picsnum); }
what think
file *buffer1 = malloc(sizeof(card));
is doing? creating buffer 4 or 8 bytes long. (the size of file * ). told read 512 bytes @ once. , char * buffer not file *. how doing.
char *buffer1 = malloc(512);
Comments
Post a Comment