c - 在 Ubuntu 14.04 上使用 curl 时未完成文件下载(段错误)

标签 c ubuntu curl download segmentation-fault

编译我的程序后,我得到这个 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;
}

最佳答案

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

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

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

关于c - 在 Ubuntu 14.04 上使用 curl 时未完成文件下载(段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704403/

相关文章:

shell - 如何检查 shell 和可能的 URL 是否存在?

c - 如何解决 Unresolved inclusion : <mongoc. h>?

node.js - 如何保持 Node 应用程序运行?

ubuntu - 更新到 PHP7 并使用新的 php.ini 文件创建新文件夹后如何获取旧的 php.ini 设置

linux - 树莓派 : SPI not working, spi_bcm2835 未使用 lsmod 显示

ubuntu - 使用 jar 文件创建 DEB 包

php - 如何使用 php curl 为特定 ip 设置主机名

c - 在将输入作为 shell 命令传递给 exe 时,可以将文件替换为缓冲区吗?

c++ - 如果我们添加安全的有符号/无符号比较 C/C++,它会破坏语言或现有代码吗?

增加 C 中自动数组的大小后出现编译器错误