C/C ++ displays the progress in the console

Source: Internet
Author: User

When a program performs a large number of operations or I/O operations, it usually takes a long time. In order not to let users have a boring wait or mistakenly think that the system has crashed, it is necessary to add a progress prompt to the program. It is very easy to display the progress bar and the percentage of completion in the window, but how can I complete this task on the console? The current completion percentage of the output of a row is definitely not beautiful enough. It is best to show the old number completely covered by the new number just as you do. In fact, this is very easy. The sample code below will be explained later.

// Display the progress # include <stdio. h> # include <windows. h> int main () {const int num = 50; // total number of tasks completed for (INT I = 0; I <num; I ++) {// use sleep () to replace some time-consuming operations, such as reading and writing files and a large number of operations. Sleep (200); // some work to do // percentage of output printf ("%. 2lf % \ r", I * 100.0/num);} return 0 ;}

It can be seen that the key is that the \ r book says it is the carriage return character in the escape character-the description is not very in place. To be accurate, we should go back to the beginning of this line.

For example, printf ("123 \ r100 \ n"); outputs 100. Printf ("12345 \ r100 \ n"); 10045 is output.

Here is an example of a complex point:

#include <stdio.h>int main(){printf("MoreWindows");printf("12345\r00\n");return 0;}

What will be output?

The first statement outputs morewindows, the second statement outputs 12345, returns to the beginning of the line, and then outputs 00 and line feed. In this way, the first two characters will be overwritten to output 00rewindows12345.

Let's look at an example. The following program will not output any content after it is run.

#include <stdio.h>int main(){printf("12345\r");return 0;}

This is because the interval between output and cleanup of the above program is too small for human eyes to notice. The following program will show you how the printf () function executes \ r.

#include <stdio.h>#include <windows.h>int main(){char szText[] = "MoreWindows";printf("12345679\r");Sleep(2000);for (int i = 0; i < strlen(szText); i++){putchar(szText[i]);Sleep(200);}putchar('\n');}

After running, you will see the output 123456789. After the cursor moves to the beginning of the line and flashes below 1, 123456789 is still on the display screen. After 2 seconds, the screen outputs morewindows in sequence and overwrites the original 123456789.

 

Conclusion: It is undoubtedly very convenient to display the progress in the console. In many cases, you can simply beautify the program and reduce the boredom of users waiting for the progress bar in the window.

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/6742078

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.