How to display a progress indicator in pure C/C++ (cout/printf)?

后端 未结 8 1367
夕颜
夕颜 2020-12-12 11:10

I\'m writing a console program in C++ to download a large file. I have known the file size, and I start a work thread to download. I want to show a progress indicator to mak

相关标签:
8条回答
  • 2020-12-12 11:35

    You can use a "carriage return" (\r) without a line-feed (\n), and hope your console does the right thing.

    0 讨论(0)
  • 2020-12-12 11:36

    Another way could be showing the "Dots" or any character you want .The below code will print progress indicator [sort of loading...]as dots every after 1 sec.

    PS : I am using sleep here. Think twice if performance is concern.

    #include<iostream>
    using namespace std;
    int main()
    {
        int count = 0;
        cout << "Will load in 10 Sec " << endl << "Loading ";
        for(count;count < 10; ++count){
            cout << ". " ;
            fflush(stdout);
            sleep(1);
        }
        cout << endl << "Done" <<endl;
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题