Ubuntu终端,从C语言控制台读取,但不将读取的文本打印在屏幕上

fnvucqvd  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(109)

我正在使用Ubuntu和C++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我已经使用了下面的代码,但我没有得到我想要的结果,因为我想看到的字符,我正在阅读:

/* get terminal attributes */
    struct termios termios;
    tcgetattr(STDIN_FILENO, &termios);

    /* disable echo */
    termios.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
vaqhlq81

vaqhlq811#

换句话说,您希望在按下Enter键后删除输入。
您可以使用ANSI转义序列完成此操作。下面是一个示例:

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

int main() {
    char phrase[100];

    printf("Enter passphrase: ");
    if (!fgets(phrase, sizeof phrase, stdin))
        return 1;
    phrase[strcspn(phrase, "\n")] = '\0'; // strip the newline if any

    printf("\033[F\r");   // back up one line
    printf("Enter passphrase: "); // output the prompt on top of itself
    printf("\033[K\r\n"); // erase end of line, move to next line

    printf("\nPass phrase is %s\n", phrase);
    return 0;
}

相关问题