Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序不显示下载速度和文件大小(卷曲)_C_Curl_Download - Fatal编程技术网

C 程序不显示下载速度和文件大小(卷曲)

C 程序不显示下载速度和文件大小(卷曲),c,curl,download,C,Curl,Download,我对Ubuntu上的curl有问题。我希望它显示下载速度和下载文件的大小。我已经声明了双倍大小、双倍速度和卷曲。我总是犯错误。程序编译到“文件下载”的那个一刻。它还应该告诉我来自CURLINFO\u SIZE\u下载和CURLINFO\u SPEED\u下载的信息。请帮忙 #define CURL_STATICLIB #include <stdio.h> #include <curl/curl.h> #include <string.h> #include &

我对Ubuntu上的curl有问题。我希望它显示下载速度和下载文件的大小。我已经声明了双倍大小、双倍速度和卷曲。我总是犯错误。程序编译到“文件下载”的那个一刻。它还应该告诉我来自CURLINFO\u SIZE\u下载和CURLINFO\u SPEED\u下载的信息。请帮忙

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.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;
    FILE *stream = stdout;
    int x;
    char y[1024];
    double* sized;
    double* speedd;
    char* name = calloc(1, 1024);
    char* outfilename;
    char* path_pdf = "/home/user/Desktop/%s.pdf";
    char* path_jpg = "/home/user/Desktop/%s.jpg";
    char* path_txt = "/home/user/Desktop/%s.txt";
    char* path_mp3 = "/home/user/Desktop/%s.mp3";
    char* path_avi = "/home/user/Desktop/%s.avi";
    printf("Enter file url: \n"); // for example http://oi58.tinypic.com/15nk3de.jpg
    scanf ("%s",y);
    char *url = y;
    printf("Type name of file \n");
    scanf("%s",name);
    char *result_pdf = malloc(strlen(path_pdf) - 2 + strlen(name) + 1);
    sprintf(result_pdf, path_pdf, name);
    char *result_jpg = malloc(strlen(path_jpg) - 2 + strlen(name) + 1);
    sprintf(result_jpg, path_jpg, name);
    char *result_txt = malloc(strlen(path_txt) - 2 + strlen(name) + 1);
    sprintf(result_txt, path_txt, name);
    char *result_mp3 = malloc(strlen(path_mp3) - 2 + strlen(name) + 1);
    sprintf(result_mp3, path_mp3, name);
    char *result_avi = malloc(strlen(path_avi) - 2 + strlen(name) + 1);
    sprintf(result_avi, path_avi, name);
    printf("Choose type of file:\n [0] - pdf\n [1] - jpg\n [2] - txt\n [3] - mp3\n [4] - avi\n "); //choose 1
    scanf("%d",&x);
    switch(x){
    case 0:
    outfilename = result_pdf;
    break;
    case 1:
    outfilename = result_jpg;
    break;
    case 2:
    outfilename = result_txt;
    break;
    case 3:
    outfilename = result_mp3;
    break;
    case 4:
    outfilename = result_avi;
    break;

}
    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);
        printf("Download started!\n\n");
        curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK) {
        printf("File downloaded!\n\n");}
        else {
        printf("Transfer failed!");}
        curl_easy_getinfo(curl,CURLINFO_SIZE_DOWNLOAD,&sized);
        fprintf(stream,"%.3f \n", sized);
        curl_easy_getinfo(curl,CURLINFO_SPEED_DOWNLOAD,&speedd);
        fprintf(stream, "%.3f \n", speedd);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;

}
#定义CURL\u STATICLIB
#包括
#包括
#包括
#包括
大小写入数据(void*ptr、大小、大小nmemb、文件*流)
{
书写的大小;
写入=写入(ptr、大小、nmemb、流);
书面回报;
}
内部主(空)
{
卷曲*卷曲;
文件*fp;
卷曲编码;
文件*stream=stdout;
int x;
chary[1024];
双倍*尺寸;
双倍*速度;
char*name=calloc(1024);
字符*输出文件名;
char*path_pdf=“/home/user/Desktop/%s.pdf”;
char*path_jpg=“/home/user/Desktop/%s.jpg”;
char*path_txt=“/home/user/Desktop/%s.txt”;
char*path_mp3=“/home/user/Desktop/%s.mp3”;
char*path_avi=“/home/user/Desktop/%s.avi”;
printf(“输入文件url:\n”);//例如http://oi58.tinypic.com/15nk3de.jpg
scanf(“%s”,y);
char*url=y;
printf(“文件类型名称”);
scanf(“%s”,名称);
char*result\u pdf=malloc(strlen(path\u pdf)-2+strlen(name)+1);
sprintf(结果、路径、名称);
char*result\u jpg=malloc(strlen(path\u jpg)-2+strlen(name)+1);
sprintf(结果、路径、名称);
char*result_txt=malloc(strlen(path_txt)-2+strlen(name)+1);
sprintf(结果文本、路径文本、名称);
char*result\u mp3=malloc(strlen(path\u mp3)-2+strlen(name)+1);
sprintf(结果、路径、名称);
char*result_avi=malloc(strlen(path_avi)-2+strlen(name)+1);
sprintf(结果、路径、名称);
printf(“选择文件类型:\n[0]-pdf\n[1]-jpg\n[2]-txt\n[3]-mp3\n[4]-avi\n”);//选择1
scanf(“%d”和&x);
开关(x){
案例0:
outfilename=结果\ uPDF;
打破
案例1:
outfilename=结果_jpg;
打破
案例2:
outfilename=result_txt;
打破
案例3:
outfilename=结果\u mp3;
打破
案例4:
outfilename=结果_avi;
打破
}
curl=curl_easy_init();
if(curl)
{
fp=fopen(出口名称,“wb”);
curl_easy_setopt(curl,CURLOPT_URL,URL);
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,write\u data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
printf(“下载已开始!\n\n”);
卷曲度(卷曲度,卷曲度(1L));
res=旋度(curl)\u容易执行(curl);
如果(res==CURLE\u OK){
printf(“已下载文件!\n\n”);}
否则{
printf(“传输失败!”;}
curl\u easy\u getinfo(curl、CURLINFO\u SIZE\u下载和调整大小);
fprintf(流,“%.3f\n”,大小);
curl\u easy\u getinfo(curl、CURLINFO\u SPEED\u下载和speedd);
fprintf(流“%.3f\n”,speedd);
旋度\轻松\清洁(旋度);
fclose(fp);
}
返回0;
}

您在问题中提供的代码有几个问题:

  • 您没有包括
    stdlib.h

  • 名称
    未分配

  • 您尚未为uri的路径部分分配存储输入的空间。调用
    scanf(3)
    读取路径名时,它会尝试将其存储到
    name
    引用的内存位置。由于
    name
    未初始化,指针的值未定义。这会导致分段错误

    name
    分配内存,或者将类型从指针更改为大小适当的数组

     char *name = calloc(1, 1024);
     printf("Type name of file \n");
     scanf("%s",name);
    

    这两种方法都能很好地满足您的需要。但是,请注意,这些方法会引入堆或堆栈溢出错误,因为
    scanf(3)
    不执行边界检查。这类事情的通常模式是使用
    fgets(3)
    读取一行输入,并使用
    sscanf(3)
    标记它。在这种情况下,您实际上只需要
    fgets(3)

    不要使用
    获取(3)

  • 您没有打印成功时的下载信息
  • 在您的代码中,您仅在出现故障时打印cURL的状态信息:

    if (res == CURLE_OK) {
        printf("File downloaded!\n\n");
    } else {
        printf("Transfer failed!");}
        curl_easy_getinfo(curl,CURLINFO_SIZE_DOWNLOAD,sized);
        curl_easy_getinfo(curl,CURLINFO_SPEED_DOWNLOAD,speedd);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    
    相反,我认为你的意思是:

    if (res == CURLE_OK) {
        printf("File downloaded!\n\n");
    } else {
        printf("Transfer failed!");}
    }
    
    curl_easy_getinfo(curl,CURLINFO_SIZE_DOWNLOAD,sized);
    curl_easy_getinfo(curl,CURLINFO_SPEED_DOWNLOAD,speedd);
    curl_easy_cleanup(curl);
    fclose(fp);
    
    我冒昧地猜测这个问题的原因是因为您的代码风格。看起来您至少是C语言的新手。使用一致的风格编写代码将提高代码的可读性,并帮助您避免类似的错误。你用什么样的风格并不重要,只要你坚持下去

  • 您的程序的行为与它所做的是不可预料的,这会增加代码的大小
  • 你的程序架构有点迟钝。更常见的是允许用户在命令行上指定参数。在本例中,您可以选择命名输出文件和命名源URI

    即使您这样做是为了学习标准库,也最好继续您的实现,以更预期的方式运行。另请参见
    getopt(3)
    手册页。您可能会发现,一旦您这样做,您的程序的大小将缩小约一半

    较小的程序是更好的程序

  • 您正在将未定义的指针值传递到
    curl\u easy\u getinfo
  • 你已经宣布

    double* sized;
    double* speedd;
    
    但没有为这些提供存储。当您调用
    curl\u easy\u getinfo
    时,它会崩溃。您在评论中提到,您通过将地址传递到
    curl\u easy\u getinfo
    解决了这个问题。这将停止崩溃,因为您已经为地址提供了存储。然而,如果你试图打印这些信息,你要么一无所获,要么再次崩溃

    相反,您想要的是:

    double sized;
    double speedd;
    ...
    curl_easy_getinfo(curl,CURLINFO_SIZE_DOWNLOAD,&sized);
    curl_easy_getinfo(curl,CURLINFO_SPEED_DOWNLOAD,&speedd);
    
  • 你没有打印出任何地方的大小或速度
  • 一旦你得到了它们,你就不能在y末端打印尺寸或速度了
    double sized;
    double speedd;
    ...
    curl_easy_getinfo(curl,CURLINFO_SIZE_DOWNLOAD,&sized);
    curl_easy_getinfo(curl,CURLINFO_SPEED_DOWNLOAD,&speedd);
    
    printf("Size: %g KB\nSpeed: %g KB/s\n", sized / 1024., speedd / 1024.);
    
    Enter file url:
    http://oi58.tinypic.com/15nk3de.jpg
    Type name of file
    foo.jpg
    Choose type of file:
     [0] - pdf
     [1] - jpg
     [2] - txt
     [3] - mp3
     [4] - avi
     1
    Download started!
    
    * Hostname was NOT found in DNS cache
    *   Trying 209.17.68.209...
    * Connected to oi58.tinypic.com (209.17.68.209) port 80 (#0)
    > GET /15nk3de.jpg HTTP/1.1
    Host: oi58.tinypic.com
    Accept: */*
    
    < HTTP/1.1 200 OK
    * Server Apache is not blacklisted
    < Server: Apache
    < Last-Modified: Thu, 13 Feb 2014 19:51:01 GMT
    < ETag: "1d7a5-4f24f024dc8be"
    < Cache-Control: max-age=21600
    < Expires: Sun, 04 Jan 2015 00:02:56 GMT
    < Content-Type: image/jpeg
    < Content-Length: 120741
    < Accept-Ranges: bytes
    < Date: Sat, 03 Jan 2015 19:24:19 GMT
    < X-Varnish: 1269536769 1268437896
    < Age: 4883
    < Via: 1.1 varnish
    < Connection: keep-alive
    < X-Varnish-Server: den2tpv65
    < X-Cache: HIT
    <
    * Connection #0 to host oi58.tinypic.com left intact
    File downloaded!
    
    Size: 117.911 KB
    Speed: 169.138 KB/s