c - Segmentation fault (core dumped) in some cases -
any idea why program fail in downloading images page #12 while did pages 1-9 perfectly? don't know how can debug. maybe there's problem wget when doesn't find first image? http://img717.imageshack.us/img717/7954/white2u.png
#include <stdio.h> #include <stdlib.h> // using system calls #include <unistd.h> // sleep int main () { char body[] = "forum-post-body-content", notes[] = "p-comment-notes", img[] = "img src=", link[200], cmd[200]={0}, file[10]; int c, pos = 0, pos2 = 0, fin = 0, i, j, num = 0, found = 0; file *fp; (i = 12; <= 149; ++i) { sprintf(cmd,"wget -o page%d.txt 'http://www.mtgsalvation.com/forums/creativity/artwork/340782-official-digital-rendering-thread?page=%d'",i,i); system(cmd); sprintf(file, "page%d.txt", i); fp = fopen (file, "r"); while ((c = fgetc(fp)) != eof) { if (body[pos] == c) { if (pos == 22) { pos = 0; while (fin == 0) { c = fgetc (fp); if (feof (fp)) break; if (notes[pos] == c) { if (pos == 14) { fin = 1; pos = -1; } ++pos; } else { if(pos > 0) pos = 0; } if (img[pos2] == c) { if (pos2 == 7) { pos2 = 0; while (found == 0) { c = fgetc (fp); // char file link[pos2] = c; if (pos2 > 0) { if(link[pos2-1] == 'g' && link[pos2] == '\"') { found = 1; } } ++pos2; } --pos2; found = 0; char link2[pos2]; (j = 1; j < pos2; ++j) { link2[j - 1] = link[j]; } link2[j - 1] = '\0'; sprintf(cmd, "wget -o /home/arturo/dropbox/digital_renders/%d \'%s\'", ++num, link2); system(cmd); pos2 = -1; } ++pos2; } else { if(pos2 > 0) pos2 = 0; } } fin = 0; } ++pos; } else pos = 0; } // closing file fclose (fp); if (remove (file)) fprintf(stderr, "can't remove file\n"); } }
char file[10];
"page12.txt"
has 11 characters in including null character. please char file[128]
. memory cheap. time spent debugging expensive.
Comments
Post a Comment