【问题标题】:Not completed downloading of file (segmentation fault) while using curl on Ubuntu 14.04在 Ubuntu 14.04 上使用 curl 时未完成文件下载(分段错误)
【发布时间】:2014-12-30 11:13:15
【问题描述】:

编译我的程序后,我得到了这个error:
我正在使用 Code::Blocks.Program 被编写为易于下载管理器。所有类型的文件(pdf、txt、jpg)都会出现问题。这是我的代码。我不知道为什么会这样。请帮忙。

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void)
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    int x;
    char y[200];
    char page;
    char* outfilename;
    char* path_pdf = "/home/user/Desktop/document.pdf";
    char* path_jpg = "/home/user/Desktop/picture.jpg";
    char* path_txt = "/home/user/Desktop/document.txt";
    char FILEPATH[3] = {path_pdf, path_jpg, path_txt};
    printf("Enter file url: \n"); // for example http://oi58.tinypic.com/15nk3de.jpg
    scanf ("%s",y);
    char *url = y;
    printf("Choose type of file:\n [0] - pdf\n [1] - jpg\n [2] - txt\n "); //choose 1
    scanf("%d",&x);
    outfilename = FILEPATH[x];
    curl = curl_easy_init();
    if (curl)
    {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
        res = curl_easy_perform(curl);
        if (res == CURLE_OK)
   {
    printf("File downloaded!\n");
   }
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

【问题讨论】:

    标签: c ubuntu curl download segmentation-fault


    【解决方案1】:
    char FILEPATH[3] = {path_pdf, path_jpg, path_txt}; 
    

    char的数组(你想要一个字符串数组),改成:

    char *FILEPATH[3] = {path_pdf, path_jpg, path_txt};

    【讨论】:

    • @davoid 你为什么忽略编译器警告?
    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2014-07-11
    • 1970-01-01
    相关资源
    最近更新 更多