开发者

C++ Change Output From "cout"

开发者 https://www.devze.com 2023-01-29 06:10 出处:网络
Is it possible to change text printed with \"cout\"? I would like to make it show the current percentage of some开发者_运维知识库thing without having to have a new line for each percentage. Is this po

Is it possible to change text printed with "cout"? I would like to make it show the current percentage of some开发者_运维知识库thing without having to have a new line for each percentage. Is this possible?


This works for me:

std::cout << "1111";
std::cout << "\r";
std::cout << "2222";

\r is a carriage return symbol. Puts the "cursor" back to the beginning of the line.

Alternatively you can use \b character. This is backspace. When printed it goes one character back.


In general it is not possible. (imagine that the output from cout is fed directly to a printer. How would you instruct it to "unprint" the last line?) cout is an output stream, it makes no assumptions about which medium the output is sent to, or about the capabilities of that medium. Specific tricks can achieve what you want in some cases, but will fail horribly in others. If you want anything more dynamic than straight output of plain text, perhaps cout isn't the right tool to use.


One thing that you will definitely not get from cout is terminal line length. As this can be changed, you might use too long lines, which (using '\r') will cause printing new lines every update. If you want to use specific platform then use platform-specific functions to obtain terminal size (mind that you might not be attached to any terminal at all, eg. redirected to file).

0

精彩评论

暂无评论...
验证码 换一张
取 消