c - 某些情况下的段错误(核心已转储)

标签 c segmentation-fault wget

知道为什么这个程序在从第 12 页下载图像时会失败,而它可以完美地完成第 1-9 页吗?我真的不知道如何调试。找不到第一张图片时,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");
    }
}

最佳答案

char file[10];

"page12.txt" 有 11 个字符,包括空字符。请只执行类似 char file[128] 的操作。内存很便宜。花在调试上的时间很昂贵。

关于c - 某些情况下的段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324331/

相关文章:

创建硬编码定义的变体

c++ - std::string::assign() 导致段错误

c++ - 预编译头是递归的吗?

c - 如何在 mingw-w64 gcc 7.1 中无警告地打印 size_t?

C X11 MandelBrot 集应用中的段错误

gitlab - 将 Gitlab 上的私有(private)仓库的单个文件公开访问

linux - wget 克隆一个网站,链接到目录而不是 index.html

php - 如何将这个wget函数转换为php?

c - Linux 内核中可能/不可能的宏如何工作以及它们的好处是什么?

c - 为什么是 "Segmentation fault(core dumped)"?