简体   繁体   English

在某些情况下,分段错误(核心已转储)

[英]Segmentation fault (core dumped) in some cases

Any idea why this program will fail in downloading images from page #12 while it did pages 1-9 perfectly? 知道为什么该程序无法完美地执行第1-9页的操作却无法从第12页下载图像吗? I really don't know how can I debug. 我真的不知道该如何调试。 Maybe there's a problem with wget when it doesn't find the first image? 找不到第一个图像时,wget可能存在问题? http://img717.imageshack.us/img717/7954/white2u.png 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");
    }
}
char file[10];

"page12.txt" has 11 characters in it including the null character. "page12.txt"包含11个字符,包括空字符。 Please just do something like char file[128] . 请只做类似char file[128] Memory is cheap. 内存很便宜。 Time spent debugging is expensive. 花在调试上的时间很昂贵。

You have an overflow. 你溢出了。

file[10]; 文件[10];

page1.txt = 10 characters including the null terminator page1.txt = 10个字符,包括空终止符

page12.txt = 11 characters page12.txt = 11个字符

Look into to the safe functions like snprintf() 研究像snprintf()这样的安全函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM