Printing dynamic value in C
I have a program that transfers a file written in C. I wanted to give the user some feed 开发者_StackOverflow中文版back on how much bytes and/or percentage has been transferred. My codes does something like this:
Transferring......100 bytes.
Transferring......200 bytes. Transferring......300 bytes. Transferring......400 bytes. Transferring......500 bytes. Transferred Complete!!!As you can see, if I transfer a large file it'll print a bunch of lines. Is it possible to have the number of bytes updates and change without printing another line?
Thanks in advance guys!
There's no way to do this generically in standard C - after all, the output of your program might be going directly to a line printer.
However, on many terminals you can print a \r
character to return the cursor to the beginning of the current line (or print a number of \b
backspace characters to erase characters one-by-one).
Use \r
instead of \n
.
If you're writing a win32 application, you can manipulate the position of the cursor with the SetConsoleCursorPosition(HANDLE, COORD)
method to overwrite specific characters:
http://msdn.microsoft.com/en-us/library/ms686025(v=VS.85).aspx
精彩评论