linux系统c语言如何实现通过链接下载文件

flashtt 2017-11-14 07:38:29
如题,不知道如何实现在linux系统中通过c语言通过链接下载文件,类似于wget的功能,不知道有没有一些库可以调用,网上找到一些自写的库感觉有些不完善,超时退出,断线退出不知道如何返回
...全文
1062 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_42116862 2018-10-12
  • 打赏
  • 举报
回复
已经试过了 可以下载文件
Oasis_maT 2018-02-27
  • 打赏
  • 举报
回复
引用 6 楼 hongwenjun 的回复:
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* <DESC>
 * Download a given URL into a local file named page.out.
 * </DESC>
 */
#include <stdio.h>
#include <stdlib.h>

/*
 * 许多在Linux下开发的C程序都需要头文件unistd.h,但VC中没有个头文件,
 * 所以用VC编译总是报错。把下面的内容保存为unistd.h,可以解决这个问题。
 */
#ifdef _WIN32
#include <io.h>
#include <process.h>
#else
#include<unistd.h>
#endif

#include <curl/curl.h>


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

// 保存URL文件内容到文件
bool url2file(const char* web_url, const char* savefile)
{
    CURL* curl_handle;
    const char* pagefilename = savefile;
    FILE* pagefile;


    curl_global_init(CURL_GLOBAL_ALL);

    /// init the curl session  初始化cURL协议
    curl_handle = curl_easy_init();

    /// set URL to get here   设置URL到这里
    curl_easy_setopt(curl_handle, CURLOPT_URL, web_url);

    /// Switch on full protocol/debug output while testing  测试时开启完整的协议/调试输出
    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L);

    /// disable progress meter, set to 0L to enable and disable debug output   禁用进度表,设置为0L启用和禁用调试输出
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);


    /// send all data to this function  所有数据发送给这个函数
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /// open the file   写文件到结果文件
    pagefile = fopen(pagefilename, "wb");
    if (pagefile) {

        /// write the page body to this file handle  写页面文件到保存文件句柄
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

        /* get it! */
        curl_easy_perform(curl_handle);

        /* close the header file */
        fclose(pagefile);
    }

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);

    return true;
}
这个应该只能下网页吧
zlf_93_02 2017-11-15
  • 打赏
  • 举报
回复
引用 2 楼 heronism 的回复:
libcurl,可以调用库或编译出来的二进制文件,绝对好用
这个库确实很强大,建议使用这个。
flashtt 2017-11-15
  • 打赏
  • 举报
回复
引用 2 楼 heronism 的回复:
libcurl,可以调用库或编译出来的二进制文件,绝对好用
你好,有在linux下用过这个库吗,我之前也看到过,但不知道怎么用,我是用在32位的嵌入式平台上,光下哪个版本就摸不着头脑
heronism 2017-11-15
  • 打赏
  • 举报
回复
libcurl,可以调用库或编译出来的二进制文件,绝对好用
hongwenjun 2017-11-15
  • 打赏
  • 举报
回复
引用 7 楼 flashtt 的回复:
[quote=引用 6 楼 hongwenjun 的回复:]
 https://curl.haxx.se/libcurl/c/url2file.html

}
这个函数好像封装的不错,这是libcurl的函数吗[/quote] 是curl 的示例改的 https://curl.haxx.se/libcurl/c/url2file.html 文件在这里
flashtt 2017-11-15
  • 打赏
  • 举报
回复
引用 6 楼 hongwenjun 的回复:
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* <DESC>
 * Download a given URL into a local file named page.out.
 * </DESC>
 */
#include <stdio.h>
#include <stdlib.h>

/*
 * 许多在Linux下开发的C程序都需要头文件unistd.h,但VC中没有个头文件,
 * 所以用VC编译总是报错。把下面的内容保存为unistd.h,可以解决这个问题。
 */
#ifdef _WIN32
#include <io.h>
#include <process.h>
#else
#include<unistd.h>
#endif

#include <curl/curl.h>


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

// 保存URL文件内容到文件
bool url2file(const char* web_url, const char* savefile)
{
    CURL* curl_handle;
    const char* pagefilename = savefile;
    FILE* pagefile;


    curl_global_init(CURL_GLOBAL_ALL);

    /// init the curl session  初始化cURL协议
    curl_handle = curl_easy_init();

    /// set URL to get here   设置URL到这里
    curl_easy_setopt(curl_handle, CURLOPT_URL, web_url);

    /// Switch on full protocol/debug output while testing  测试时开启完整的协议/调试输出
    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L);

    /// disable progress meter, set to 0L to enable and disable debug output   禁用进度表,设置为0L启用和禁用调试输出
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);


    /// send all data to this function  所有数据发送给这个函数
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /// open the file   写文件到结果文件
    pagefile = fopen(pagefilename, "wb");
    if (pagefile) {

        /// write the page body to this file handle  写页面文件到保存文件句柄
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

        /* get it! */
        curl_easy_perform(curl_handle);

        /* close the header file */
        fclose(pagefile);
    }

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);

    return true;
}
这个函数好像封装的不错,这是libcurl的函数吗
hongwenjun 2017-11-15
  • 打赏
  • 举报
回复
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/
/* <DESC>
 * Download a given URL into a local file named page.out.
 * </DESC>
 */
#include <stdio.h>
#include <stdlib.h>

/*
 * 许多在Linux下开发的C程序都需要头文件unistd.h,但VC中没有个头文件,
 * 所以用VC编译总是报错。把下面的内容保存为unistd.h,可以解决这个问题。
 */
#ifdef _WIN32
#include <io.h>
#include <process.h>
#else
#include<unistd.h>
#endif

#include <curl/curl.h>


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

// 保存URL文件内容到文件
bool url2file(const char* web_url, const char* savefile)
{
    CURL* curl_handle;
    const char* pagefilename = savefile;
    FILE* pagefile;


    curl_global_init(CURL_GLOBAL_ALL);

    /// init the curl session  初始化cURL协议
    curl_handle = curl_easy_init();

    /// set URL to get here   设置URL到这里
    curl_easy_setopt(curl_handle, CURLOPT_URL, web_url);

    /// Switch on full protocol/debug output while testing  测试时开启完整的协议/调试输出
    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L);

    /// disable progress meter, set to 0L to enable and disable debug output   禁用进度表,设置为0L启用和禁用调试输出
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);


    /// send all data to this function  所有数据发送给这个函数
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /// open the file   写文件到结果文件
    pagefile = fopen(pagefilename, "wb");
    if (pagefile) {

        /// write the page body to this file handle  写页面文件到保存文件句柄
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

        /* get it! */
        curl_easy_perform(curl_handle);

        /* close the header file */
        fclose(pagefile);
    }

    /* cleanup curl stuff */
    curl_easy_cleanup(curl_handle);

    return true;
}
CT8100 2017-11-15
  • 打赏
  • 举报
回复
看看这个怎么样,我记得下载下来里面都是有用法教程的。。http://blog.csdn.net/u012467749/article/details/50740006
自信男孩 2017-11-14
  • 打赏
  • 举报
回复
试一下system,比如system("ls -l");类似bash执行命令,可以将wget命令放到system函数参数里。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧