I am using Ubuntu and C with VS Code, I want to read a text from stdin but after I am done reading I don't want the text I read to appear on the screen/terminal. Also I want to see the letters that I am reading but after I am done, I don't want to see the string on the screen.
I have used the code below but I am not getting the results I want as I want to see the characters that I am reading:
/* get terminal attributes */
struct termios termios;
tcgetattr(STDIN_FILENO, &termios);
/* disable echo */
termios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios);
CodePudding user response:
To rephrase your purpose, you want the input to be erased after you hit the Enter key.
You can do this using ANSI escape sequences. Here is an example:
#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;
}