C中使用管道的大写字母
所以基本上这是一个任务,即:使用进程之间的通信,自动将大写字母转换为小写字母,将小写字母转换为大写字母,而不让它们出现在输入中。基本上我看不到我的输入,只看到输出。当按下CTRL+C时,程序应该识别它,并要求用户键入y以进行确认。 我的程序正在运行,但当我从printf%c\n中删除\n时,upper;从printf%c\n开始,向下;节目开始变得很怪异。。。当我删除它时,我必须按enter键才能看到输出,但它会自动显示,就像我想要的一样。。。有人能解释一下为什么吗 我正在使用ubuntu。printf实际上并没有立即打印到stdout,它只是将字符放入输出缓冲区,以便稍后刷新到stdout。如果输出为终端,则通常在打印换行符时显示后一点,但您可以使用setbuf进行更改:C中使用管道的大写字母,c,process,pipe,C,Process,Pipe,所以基本上这是一个任务,即:使用进程之间的通信,自动将大写字母转换为小写字母,将小写字母转换为大写字母,而不让它们出现在输入中。基本上我看不到我的输入,只看到输出。当按下CTRL+C时,程序应该识别它,并要求用户键入y以进行确认。 我的程序正在运行,但当我从printf%c\n中删除\n时,upper;从printf%c\n开始,向下;节目开始变得很怪异。。。当我删除它时,我必须按enter键才能看到输出,但它会自动显示,就像我想要的一样。。。有人能解释一下为什么吗 我正在使用ubuntu。pr
#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);
}
之后,每次调用printf都会立即刷新缓冲区。或者,您可以使用fflushstdout在程序中的任何特定点刷新缓冲区。正是我要找的,setbufstdout,0;解决了
setbuf(stdout, 0); // set stdout to unbuffered