将代码卷曲成C中的变量
我使用两个线程,一个正在下载,另一个应该检查下载了多少字节 以下是我的程序的确切代码:将代码卷曲成C中的变量,c,curl,pthreads,C,Curl,Pthreads,我使用两个线程,一个正在下载,另一个应该检查下载了多少字节 以下是我的程序的确切代码: #include <stdio.h> #include <curl/curl.h> #include <curl/easy.h> #include <string.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <un
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
CURLcode res;
FILE *fp;
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;
}
void *downloadThread() {
CURL *curl;
char *url = "http://imgsrc.hubblesite.org/hu/db/images/hs-2006-10-a-hires_jpg.jpg";
char outfilename[FILENAME_MAX] = "picture.jpg";
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("File download started\n");
res = curl_easy_perform(curl);
printf("File download finished\n");
curl_easy_cleanup(curl);
//fclose(fp);
}
}
void *checkThread() {
while(1) {
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int downloadedFile=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
//int downloadedFile = 0; /* instead of 0 it should be something with "res" variable */
printf("The file size is %d\n", downloadedFile);
usleep(1000000);
}
}
void setThread() {
//Thread settings
pthread_t tid1, tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1,&attr,downloadThread, NULL);
pthread_create(&tid2,&attr,checkThread, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
}
int main() {
setThread();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
卷曲编码;
文件*fp;
大小写入数据(void*ptr、大小、大小nmemb、文件*流){
书写的大小;
写入=写入(ptr、大小、nmemb、流);
书面回报;
}
void*下载线程(){
卷曲*卷曲;
char*url=”http://imgsrc.hubblesite.org/hu/db/images/hs-2006-10-a-hires_jpg.jpg";
char outfilename[FILENAME_MAX]=“picture.jpg”;
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”);
res=旋度(curl)\u容易执行(curl);
printf(“文件下载完成\n”);
旋度\轻松\清洁(旋度);
//fclose(fp);
}
}
void*checkThread(){
而(1){
int prev=ftell(fp);
fseek(fp,0L,SEEK_END);
int downloadedFile=ftell(fp);
fseek(fp,prev,SEEK_SET);//回到我们原来的位置
//int downloadedFile=0;/*而不是0,它应该是带有“res”变量的内容*/
printf(“文件大小为%d\n”,已下载文件);
美国LEEP(1000000);
}
}
void setThread(){
//线程设置
pthread_t tid1,tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1,&attr,downloadThread,NULL);
pthread_create(&tid2,&attr,checkThread,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
}
int main(){
setThread();
返回0;
}
这个函数给出了我想要的结果,但我不想保存到文件中。像这样修改write\u函数怎么样
time_t start_time = time(0);
size_t bytes_downloaded = 0;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
time_t current_time = time(0);
time_t elapsed_time = current_time - start_time;
// do you still need it?
// size_t written;
// written = fwrite(ptr, size, nmemb, stream);
bytes_downloaded += (size * nmemb);
printf("Bytes downloaded %u in %u seconds at %u bytes/sec\n",
bytes_downloaded, elapsed_time, bytes_downloaded / elapsed_time);
return (size * nmemb);
}
像这样修改write_函数怎么样
time_t start_time = time(0);
size_t bytes_downloaded = 0;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
time_t current_time = time(0);
time_t elapsed_time = current_time - start_time;
// do you still need it?
// size_t written;
// written = fwrite(ptr, size, nmemb, stream);
bytes_downloaded += (size * nmemb);
printf("Bytes downloaded %u in %u seconds at %u bytes/sec\n",
bytes_downloaded, elapsed_time, bytes_downloaded / elapsed_time);
return (size * nmemb);
}
libcurl
site有一个例子可以做到这一点:libcurl
site有一个例子可以做到这一点:这不是我在下载过程中试图检查的解决方案。然而,qrdl发布的链接实际上解决了我的问题每次收到一块数据时都会调用write_数据,因此,统计数据会不断更新。如果您需要每秒打印一次,您可以将下载的字节和经过的时间从本地转换为全局,并在第二个线程中打印。这不是解决方案,因为我正在下载过程中尝试检查。然而,qrdl发布的链接实际上解决了我的问题每次收到一块数据时都会调用write_数据,因此,统计数据会不断更新。如果您需要每秒打印一次,您可以将下载的字节和经过的时间从本地转换为全局,并在第二个线程中打印它们。