【问题标题】:Why is the ouput having unnecessary blank spaces preceeding every line?为什么输出的每一行前都有不必要的空格?
【发布时间】:2018-04-03 12:17:11
【问题描述】:

我编写了一个 C++ 程序来打印用户在按下回车键后输入的所有内容,因此在此之前他不会看到他输入的内容。下面是代码:

#include <iostream>
#include <stdio.h>
#include <ncurses.h>
#include <string.h>

using namespace std;

int main() {
    char c, printText[10000];
    int key, i;

    initscr();
    refresh();
    noecho();

    while(1) {
        c = getch();
        key = c;        
        if( key == 10 ) {
            cout<< printText << endl;
            memset( printText, 0, sizeof(printText) );
            i=0;
        }
        else 
            printText[i++] = c;
     }

    endwin();
}

此代码给出以下输出:

根据要求的输出仅在用户按下回车后才会出现。但是正如您所看到的,从输出的第二行开始的每一行之前都会出现空格,这是不需要的。我无法理解为什么输出中会出现这些空格,我应该如何避免这种情况?请指导。

【问题讨论】:

  • 使用 ncurses 函数进行输出。我的猜测是initscr 更改了终端设置,使得\n 只执行换行,而不是回车。
  • 哪个平台或操作系统?例如,Linux 使用字符 10 来表示 换行符,它通常是回车和换行。在 Windows 中,您需要一对 13、10(回车和换行)来表示 换行

标签: c++ stdout cout ncurses


【解决方案1】:

您将 curses 输出和 iostream 输出混合到同一设备。使用其中一种,否则你会发生不可预知的奇怪事情,比如你所看到的。

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    相关资源
    最近更新 更多