gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:49 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com