Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:55:42+00:00 2026-06-03T23:55:42+00:00

Here I implemented code for file download from server. its working fine. Now I

  • 0

Here I implemented code for file download from server. its working fine.
Now I want to make my own progress bar function which calculates some data like remaining seconds data Rate per second etc.
So from here I found one way to use curl progress bar option. how we can enable this option.
I completely done with this.

I put my code below. here in this code my_progress_func calls frequently as per curl library time interval. I want to change this interval time and make it to 1 second. is it possible in curl library using to set some options for curl library?

I want to call this my_progress_func function after every 1 second.

Code :

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

long test =0;

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

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, out->iAppend ? "ab":"wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  out->iAppend += nmemb;
  return fwrite(buffer, size, nmemb, out->stream);
}

int my_progress_func(void *bar,
                     double t, /* dltotal */ 
                     double d, /* dlnow */ 
                     double ultotal,
                     double ulnow)
{
    printf("%f : %f \n", d, t);

  return 0;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  int c;

  struct FtpFile ftpfile={
    "dev.zip", /* 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,
                     "sftp://root:xyz_@192.170.10.1/mnt/xyz.tar");


    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L);


    /* 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);


    curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");

    /* Switch on full protocol/debug output */

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);

    res = curl_easy_perform(curl);

    printf("res is %d\n, data get %ld\n", res, ftpfile.iAppend);


    ///Retry upto 100 times it timeout or connection drop occur
    for (c = 0; (res != CURLE_OK) && (c < 100); c++) {



        curl_easy_setopt(curl, CURLOPT_RESUME_FROM , ftpfile.iAppend);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK) c =0;
        printf("%d res is %d\n, data get %ld\n",c, res, ftpfile.iAppend);

    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */
  curl_global_cleanup();
   return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:55:44+00:00Added an answer on June 3, 2026 at 11:55 pm

    According to the curl documentation:
    http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

    Function pointer that should match the curl_progress_callback
    prototype found in . This function gets called by libcurl
    instead of its internal equivalent with a frequent interval during
    operation (roughly once per second or sooner)
    no matter if data is
    being transfered or not. Unknown/unused argument values passed to the
    callback will be set to zero (like if you only download data, the
    upload size will remain 0). Returning a non-zero value from this
    callback will cause libcurl to abort the transfer and return
    CURLE_ABORTED_BY_CALLBACK.

    If it’s calling too frequently then you can use time() and a static var to limit this, something like this:

    static time_t prevtime;
    time_t currtime;
    double dif;
    static int first = 1;
    if(first) {
        time(&prevtime);
        first = 0;
    }
    time(&currtime);
    dif = difftime(currtime, prevtime);
    if(dif < 1.0)
        return;
    prevtime = currtime;
    

    Obviously, you run the risk that curl might not call this function again for fully another second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have implemented code like here but it's not working for iOS 5.0, it
Hi have implemented simple file exchange over a client/server connection in c++. Works fine
I implemented a custom Profile object in code as described by Joel here: How
I'm making in house application that download data via xml files from external server.
I have this code here, however i want to limit the speed the user
I want to read value from .ini file. Then write a condition - if
I have implemented a file selector with a combobox. I want to write the
I have been trying to follow this example (download the source code from a
I have some code implemented in my myDocument.m file that simply attempts to load
I have to make a web service client from an wsdl file. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.