je testais this le code du manuel GNU libc:Pourquoi je dois encore appuyer sur « Enter » afin de laisser l'entrée être lu et sortie même en mode non canonique
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
/* Use this variable to remember original terminal attributes. */
struct termios saved_attributes;
void
reset_input_mode (void)
{
tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}
void
set_input_mode (void)
{
struct termios tattr;
char *name;
/* Make sure stdin is a terminal. */
if (!isatty (STDIN_FILENO))
{
fprintf (stderr, "Not a terminal.\n");
exit (EXIT_FAILURE);
}
/* Save the terminal attributes so we can restore them later. */
tcgetattr (STDIN_FILENO, &saved_attributes);
atexit (reset_input_mode);
/* Set the funny terminal modes. */
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}
int
main (void)
{
char c;
set_input_mode();
while (1)
{
read (STDIN_FILENO, &c, 1);
if (c == '\004') /* C-d */
break;
else
putchar (c);
}
return EXIT_SUCCESS;
}
Même si le terminal a été paramétré en mode non-canonique, je dois encore appuyer sur entrée pour l'entrée à recevoir Cependant
, si je change. putchar(c)
-write(STDOUT_FILENO, &c, sizeof(char))
, cela fonctionne bien que je pensais
Vous pouvez mentionner 'setvbuf (stdout, 0, _IONBF, 0)' comme moyen d'obtenir immédiatement l'écho de 'putchar()'. Ou peut-être que je l'ai fait et c'est suffisant. (Vous voudrez probablement penser à faire quelque chose pour vous convaincre que les caractères que vous voyez sur la sortie ne sont pas ce que vous voyez habituellement en écho avec le terminal, comme en utilisant 'putchar ('['), putchar (c), putchar (']'); 'à la place de' putchar (c); '.) –