问题描述
hey我看到的控制台应用程序打印颜色和看到的应用程序,如ffmpeg打印文本本身,而不是一个新行。如何在现有行上打印?我想在我的控制台应用程序中显示fps在顶部或底部,并有正常printfs去那里,并正常滚动
hey guys i seen console apps print colors and seen apps such as ffmpeg print text over itself instead of a new line. How do i print over an existing line? i want to display fps in my console app either at the very top or very bottom and have regular printfs go there and scroll normally
-edit-
我需要这对于Windows,但这是意味着跨平台,所以我最终会有一个linux和mac实现
-edit-I need this for windows but this is meant to be cross platform so i'll eventually have a linux and mac implementation
推荐答案
简单的可能性在Linux和Windows上工作,但只有一行:
There is two simple possibilities which work on linux as well as windows, but only for one line:
-
printf b);
将返回一个字符,所以你可能会计算你要退格的字符数量,并在循环中触发,或者你知道你只写n个数字, >printf(\b\b\b\b\b\b\b\b\b\b);
-
printf(要被下一个printf \r覆盖的文本);
这将使光标返回线,所以任何下一个printf都会覆盖它。请确保写入相同长度或更长的字符串,以便完全覆盖。
printf("\b");
will return for one character, so you might count how many character you want to backspace and fire this in a loop, or you know that you only write n numbers and do it likeprintf("\b\b\b\b\b\b\b\b\b\b");
printf("text to be overwritten by next printf\r");
this will return the cursor to the beginning of the line, so any next printf will overwrite it. Make sure to write a string of same length or longer so you overwrite it entirely.
如果要重写多行,没有像这样可移植的,几乎在每个操作系统上都有libs,而你不需要处理ANSI差异。
If you want to rewrite several lines, there is nothing so portable as ncurses, there is libs for it on practically every operating system, and you don't have to take care of the ANSI-differences.
编辑:添加链接到ncurses维基百科页面,提供了很好的概述和介绍,以及链接列表和可能翻译成您偏好的语言
edit: added link to ncurses wikipedia page, gives great overview and introduction, as well as link list and maybe a translation to your preferred language
这篇关于控制台打印无滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!