0
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
static struct termios old, new;
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
new = old; /* make new settings same as old settings */
new.c_lflag &= ~ICANON; /* disable buffered i/o */
new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}
void INThandler(int);
void INThandler(int sig)
{
char c;
signal(sig, SIG_IGN);
printf("\n Ctrl+c foi detetado, clique y para confirmar \n");
c = getchar();
if(c == 'y' || c == 'Y')
exit(0);
else
signal(SIGINT, INThandler);
getchar();
}
int main()
{
signal(SIGINT, INThandler);
int fd[2];
char readbuffer[80];
pipe(fd);
int pid = fork();
char ch;
if(pid < 0)
{
printf("\n Erro");
exit(1);
}
else if(pid == 0)
{
close(fd[0]);
do
{
ch = getch();
write(fd[1], &ch, sizeof(ch));
} while(ch != '\n');
getchar();
}
else
{
close(fd[1]);
while(1)
{
read(fd[0], readbuffer, sizeof(readbuffer));
char upper = toupper(readbuffer[0]);
char down = tolower(readbuffer[0]);
if(readbuffer[0] != upper)
{
printf("%c \n", upper);
}
else
{
printf("%c \n", down);
}
}
}
return(0);
}
所以基本上這是一個任務:使用進程之間的通信,自動將大寫字母轉換爲小寫字母,將小寫字母轉換爲大寫字母,而不會出現在輸入中。基本上我沒有看到我的輸入只是輸出。當CTRL + C被擊中時,程序應該識別它並要求用戶輸入「y」來要求確認。 我的程序正在運行,但是當我從「printf(」%c \ n「,上部)刪除\ n時;」和「printf(」%c \ n「,下);」該程序開始動作怪異的......當我刪除它,我必須按回車看到輸出,但它隨着它自動出現,就像我想...可以有人解釋我爲什麼?在C中使用管道的大寫字母
我使用的是Ubuntu。
正是我在找什麼,setbuf(stdout,0);解決了它 –