gpt4 book ai didi

C++ libcurl -- 从 ftp 下载一个 txt 文件并保存为字符串

转载 作者:行者123 更新时间:2023-11-30 04:15:25 30 4
gpt4 key购买 nike

我正在从 Java 切换到 C++,这真的很痛苦。我坚持从 ftp 下载文件。我正在尝试下载一个包含一些文本的文本文件,我只想将此文本保存为字符串而不保存文件。

我从 libcurl 站点获得了以下代码:

   #include <stdio.h>
#include <curl/curl.h>


struct FtpFile {
const char *filename;
FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;

if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}


int main(void)
{
CURL *curl;
CURLcode res;
struct FtpFile ftpfile={
"curl.tar.gz", /* name to store the file as if succesful */
NULL
};

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl) {

curl_easy_setopt(curl, CURLOPT_URL,
"ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);

/* always cleanup */
curl_easy_cleanup(curl);

if(CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}

if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */

curl_global_cleanup();

return 0;
}

不幸的是,它的作用是提供大量关于我的连接状态和文件本身的信息,但实际上并没有给我文件的内容。我究竟做错了什么?另外,如何将内容保存在变量中?

非常非常感谢!

最佳答案

不是将回调函数传递给将检索到的数据写入文件的 libcurl,而是传递一个将数据附加到字符串的函数(未测试):

#include <iostream>
#include <string>
#include <curl/curl.h>

static size_t my_write(void *buffer, size_t size, size_t nmemb, void *param)
{
std::string& text = *static_cast<std::string*>(param);
size_t totalsize = size * nmemb;
text.append(static_cast<char*>(buffer), totalsize);
return totalsize;
}

int main()
{
std::string result;
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT");
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);

/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);

/* always cleanup */
curl_easy_cleanup(curl);

if(CURLE_OK != res) {
/* we failed */
std::cerr << "curl told us " << res << '\n';
}
}
curl_global_cleanup();

std::cout << result << "\n\n";
}

关于C++ libcurl -- 从 ftp 下载一个 txt 文件并保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283982/

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