davoid
davoid

Reputation: 327

Not completed downloading of file (segmentation fault) while using curl on Ubuntu 14.04

After compiling my program i get this error:
I'm using Code::Blocks.Program is written to be easy download manager. Problem occurs with all types of files (pdf,txt,jpg). Here's my code. I don't know why is it happening. Please help.

#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;
}

Upvotes: 2

Views: 205

Answers (1)

David Ranieri
David Ranieri

Reputation: 41045

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

Is an array of char's (you want an array of strings), change to:

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

Upvotes: 2

Related Questions