开发者

printf console formatting

开发者 https://www.devze.com 2023-02-22 00:18 出处:网络
I want to make a console display with print开发者_运维技巧f, where periodically I get some inputs on 3 channels, and I wanted to print lines like :

I want to make a console display with print开发者_运维技巧f, where periodically I get some inputs on 3 channels, and I wanted to print lines like :

Channel1 Last_message_1
Channel2 Last_message_2
Channel3 Last_message_3

and when a new message comes on channel2 I want to overwrite that part of the console. Like:

Channel1 Last_message_1
Channel2 New_message_2
Channel3 Last_message_3

I know this sort of stuff can be done with printf, but I don't remember how. Any pointers ?


This post might be useful:

print to screen from c console application overwriting current line

in particular, answer #2 (not the selected answer)


As far as I know you can only change the last line with printf, and here you want to change any line, so I think you will need to look into ncurses.


You can't do this portably with printf. If your console supports it, you can send it ANSI control codes to position the cursor -- but the ANSI control codes are fairly clumsy, and quite a few "consoles" just don't support them, in which case you'll get a lot of extra garbage with the data you're trying to produce.

That leaves using something that's at least theoretically non-portable. If portability still matters, my immediate choice among those would probably be ncurses -- it's reasonably decently design, fairly easy to use, and reasonably portable.

If I was sure portability didn't matter at all and I was writing (for example) purely for Windows, it'd be worth considering using the native console functions. It's open to argument that this is rarely a very good tradeoff though -- you lose all portability, and gain only a little speed, etc.


printf ( "\033[2;1H");     // move to 2nd line
0

精彩评论

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