仅清除部分控制台输出 [英] Clearing only part of the console output

查看:177
本文介绍了仅清除部分控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

光标最初在屏幕的左上角闪烁:

The cursor is initially blinking on the top left corner of the screen:

剩余

160个字符
_

160 characters remaining
_

当我按'i'时:

剩余

159个字符

159 characters remaining

i

当我按'a'时:

剩余

剩余158个字符

158 characters remaining

ia

当我按'm'时:

剩余

157个字符

157 characters remaining

iam

以此类推.

需要做什么(根据我的说法):

  1. 只需要清除屏幕的前三个字符.
  2. 更新屏幕上新按下的键

我尝试过的事情:

我试图清除整个屏幕,然后将以前的所有内容写回到屏幕上.

I tried to clear the whole screen and write everything that was there previously back on it.

为什么我对自己的工作不满意:

因为它看起来很生涩.而且输入不顺利.

Because it gives a jerky appearance. And the entry is not smooth.

我需要您帮助我做什么:

某些内置功能或某些其他技术只能清除屏幕的一部分.

Some inbuilt function or some other technique to clear only part of the screen.

我的规格:

Windows XP SP 3

Windows XP SP 3

IDE:Visual C ++ 2010 Express

IDE: Visual C++ 2010 Express

推荐答案

首先要了解的是,C ++没有将屏幕作为语言的标准组成部分.标准输出可能是文件,打印机和cout不知道它们之间的区别.

The first thing to understand is that C++ has no conception of a screen, as a standard part of the language. Standard output might be a file, a printer and cout doesn't know the difference.

但是,屏幕设备"本身通常更智能一些,并且可以识别某些命令.其中最广泛实现的是'\ r'-回车符和'\ n'-换行符. '\ r'将光标移至该行的开头,'\ n'移至下一行,但这已不符合您的需要,因为您已经尝试过了.

The screen "device" itself, however, is typically a little smarter, and recognizes some commands. The most widely-implemented of these are '\r' - the carriage return and '\n' - the line feed. '\r' moves the cursor to the beginning of the line, and '\n' advances to the next line, but that's not fit into your needs as you've already tried.

这里看来,唯一的解决方法是使用curses(ncurses只是一种实现,尽管是Linux中的标准实现).它为您提供了一个虚拟屏幕,其中包含用于更新它们的各种命令.然后,它仅接受更改的部分,并以优化的方式更新终端.

It seems the only way forward here is to use curses (of which ncurses is only one implementation, though the standard one in Linux). It presents you with a virtual screen with various commands to update them. It then takes only the changed portions, and updates the terminal in an optimized way.

这只是使用ncurses的典型C程序的示例,值得一看:

It's just an example of the typical C program using ncurses, could be worth to take a look:

#include <ncurses.h>

int main()
{   
    int ch;

    initscr();              /* Start curses mode        */
    raw();                  /* Line buffering disabled  */
    keypad(stdscr, TRUE);   /* We get F1, F2 etc..      */
    noecho();               /* Don't echo() while we do getch */

    printw("Type any character to see it in bold\n");
    ch = getch();           /* If raw() hadn't been called
                             * we have to press enter before it
                             * gets to the program      */

    printw("The pressed key is ");
    attron(A_BOLD);
    printw("%c", ch);
    attroff(A_BOLD);

    refresh();          /* Print it on to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */

    return 0;
}

printw()函数将写入虚构"屏幕.它将内容放入缓冲区并更新一些标志,并进行其他内部ncurses簿记.它实际上并没有在实际屏幕(控制台窗口)中写入任何内容.

The printw() function writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).

您可以执行任意数量的printw()编写,但是直到您的程序执行其他操作导致虚构"屏幕缓冲区内容进入实际屏幕时,这些内容才显示在实际屏幕上

You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.

可以通过printw()缓冲区更新实际屏幕的一件事是refresh()(就像上面的源代码示例一样).

One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the source code example above does).

这篇关于仅清除部分控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆