我正在使用curl库将数据从URL复制到文件中。我希望在写入文件的同时访问该文件。但是文件并没有被创建。
CURL *curl;
CURLcode res;
static const char *filename = "stream.txt";
FILE *fileptr;
/* URL of the radio-station */
char * webaddr = "http://mp3channels.webradio.antenne.de:80/rockantenne-deutschland.aac";
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, webaddr);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
res = curl_easy_perform(curl);
fileptr = fopen(filename, "wb");
if (fileptr)
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fileptr);
curl_easy_perform(curl);
fclose(fileptr);
}
if (res != CURLE_OK)
fprintf(stderr,
"curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}发布于 2019-09-04 20:49:27
当您设置CURLOPT_WRITEFUNCTION时,您将使libcurl将所有接收到的数据传递给给定的回调,而不是默认的函数(即fwrite())。
因此,为了同时“使用”数据并将其存储在文件中,只需让您的回调函数做到这一点!
或者,您可以不设置CURLOPT_WRITEFUNCTION,而只使用CURLOPT_WRITEDATA设置FILE * (但是,如果您在Windows上使用libcurl作为DLL,则此方法不起作用)。
https://stackoverflow.com/questions/57788635
复制相似问题