Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 某些情况下的分段故障(堆芯倾倒)_C_Segmentation Fault_Wget - Fatal编程技术网

C 某些情况下的分段故障(堆芯倾倒)

C 某些情况下的分段故障(堆芯倾倒),c,segmentation-fault,wget,C,Segmentation Fault,Wget,你知道为什么这个程序无法从第12页下载图像,而它却能完美地从第1-9页下载图像吗?我真的不知道如何调试。当wget找不到第一张图像时,它可能有问题 #包括 #包含//以使用系统调用 #包括//用于睡眠 int main() { char body[]=“论坛帖子正文内容”,notes[]=“p-comment-notes”,img[]=“img src=”,link[200],cmd[200]={0},文件[10]; int c,pos=0,pos2=0,fin=0,i,j,num=0,foun

你知道为什么这个程序无法从第12页下载图像,而它却能完美地从第1-9页下载图像吗?我真的不知道如何调试。当wget找不到第一张图像时,它可能有问题

#包括
#包含//以使用系统调用
#包括//用于睡眠
int main()
{
char body[]=“论坛帖子正文内容”,notes[]=“p-comment-notes”,img[]=“img src=”,link[200],cmd[200]={0},文件[10];
int c,pos=0,pos2=0,fin=0,i,j,num=0,found=0;
文件*fp;
对于(i=12;i=0)
pos=0;
}
if(img[pos2]==c)
{
if(pos2==7)
{
pos2=0;
while(找到==0)
{
c=fgetc(fp);//从文件中获取字符
链路[pos2]=c;
如果(pos2>0)
{
if(link[pos2-1]='g'&&link[pos2]=='\'”)
{
发现=1;
}
}
++pos2;
}
--pos2;
发现=0;
char-link2[pos2];
对于(j=1;j0)
pos2=0;
}
}
fin=0;
}
++pos;
}
其他的
pos=0;
}
//关闭文件
fclose(fp);
如果(删除(文件))
fprintf(stderr,“无法删除文件”\n);
}
}
“page12.txt”
包含11个字符,其中包括空字符。请执行类似于
字符文件[128]
的操作。内存很便宜。花费调试的时间很昂贵。

您有溢出

文件[10]

page1.txt=10个字符,包括空终止符

page12.txt=11个字符


查看安全函数,如snprintf()

看起来您是从第12页开始的,您是指第24页吗?不,我更改了代码,以便在快速发现问题时开始。在第10页中,也有相同的问题,但第一张图像没有。那么,哪一行seg出现故障?该“图像”链接应该显示什么?我收到了一些广告。(如果它是一个显示文本输出的屏幕截图:请不要将其作为图像开始。)您是否在调试器中运行该程序?
#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];