2014-12-05 44 views
0

任何想法爲什麼在完成第1-9頁時,該程序將無法從第#12頁下載圖像?我真的不知道我該如何調試。也許有一個wget的問題,當它沒有找到第一個圖像? http://img717.imageshack.us/img717/7954/white2u.png在某些情況下,分段錯誤(核心轉儲)

#include <stdio.h> 
#include <stdlib.h> // for using system calls 
#include <unistd.h> // for 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; 

    for (i = 12; i <= 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); // get char from file 
            link[pos2] = c; 
            if (pos2 > 0) 
            { 
             if(link[pos2-1] == 'g' && link[pos2] == '\"') 
             { 
             found = 1; 
             } 
            } 
            ++pos2; 
           } 
           --pos2; 
           found = 0; 
           char link2[pos2]; 
           for (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"); 
    } 
} 
+0

它看起來像你開始用12頁你的意思是第24頁? – Hogan 2014-12-05 20:43:10

+0

不,我在快速發現問題時將代碼更改爲開始。在第10頁中也有同樣的問題,但不是第一個圖像。 – Arturo 2014-12-05 20:44:09

+0

那麼哪條線路故障? – Hogan 2014-12-05 20:45:04

回答

2
char file[10]; 

"page12.txt"中有11個字符,包括空字符。請僅做點像char file[128]。內存很便宜。花費在調試上的時間很長。

0

你有溢出。

file [10];

page1.txt = 10個字符,包括空終止

page12.txt = 11個字符

查找到像的snprintf)的安全功能(