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

  • Home
  • SEARCH
  • 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 3234476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:23:45+00:00 2026-05-17T17:23:45+00:00

I’m writing an SDL application for Linux, that works from the console (no X

  • 0

I’m writing an SDL application for Linux, that works from the console (no X server). One function I have is a file copy mechanism, that copies specific files from HDD to USB Flash device, and showing progress of this copy in the UI. To do this, I’m using simple while loop and copying file by 8kB chunks to get copy progress. The problem is, that it’s slow. I get to copy a 100 MB file in nearly 10 minutes, which is unacceptable.

How can I implement faster file copy? I was thinking about some asynchronous API that would read file from HDD to a buffer and store the data to USB in separate thread, but I don’t know if I should implement this myself, because it doesn’t look like an easy task. Maybe you know some C++ API/library that can that for me? Or maybe some other, better method?

  • 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-05-17T17:23:45+00:00Added an answer on May 17, 2026 at 5:23 pm

    Don’t synchronously update your UI with the copy progress, that will slow things down considerably. You should run the file copy on a separate thread from the main UI thread so that the file copy can proceed as fast as possible without impeding the responsiveness of your application. Then, the UI can update itself at the natural rate (e.g. at the refresh rate of your monitor).

    You should also use a larger buffer size than 8 KB. Experiment around, but I think you’ll get faster results with larger buffer sizes (e.g. in the 64-128 KB range).

    So, it might look something like this:

    #define BUFSIZE (64*1024)
    
    volatile off_t progress, max_progress;
    
    void *thread_proc(void *arg)
    {
        // Error checking omitted for expository purposes
        char buffer[BUFSIZE];
        int in = open("source_file", O_RDONLY);
        int out = open("destination_file", O_WRONLY | O_CREAT | O_TRUNC);
    
        // Get the input file size
        struct stat st;
        fstat(in, &st);
    
        progress = 0;
        max_progress = st.st_size;
    
        ssize_t bytes_read;
        while((bytes_read = read(in, buffer, BUFSIZE)) > 0)
        {
            write(out, buffer, BUFSIZE);
            progress += bytes_read;
        }
    
        // copy is done, or an error occurred
        close(in);
        close(out);
    
        return 0;
    }
    
    void start_file_copy()
    {
        pthread_t t;
        pthread_create(&t, NULL, &thread_proc, 0);
    }
    
    // In your UI thread's repaint handler, use the values of progress and
    // max_progress
    

    Note that if you are sending a file to a socket instead of another file, you should instead use the sendfile(2) system call, which copies the file directly in kernel space without round tripping into user space. Of course, if you do that, you can’t get any progress information, so that may not always be ideal.

    For Windows systems, you should use CopyFileEx, which is both efficient and provides you a progress callback routine.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but

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.