ストリーム内の文字を検索し、データを消費せずにストリームにプッシュバックしたい。 私はfgetcを使用していますが、プログラムは呼び出し側のfgetcで止まっています。ストリーム内の文字を検索し、パイプを使用してストリームをプッシュバックする方法
以下は私のテストプログラムです。
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
int dummy;
fd_set set;
struct timeval timeout;
FD_ZERO (&set);
FD_SET (fd[0], &set);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if (select (fd[0]+1, &set, NULL, NULL, &timeout))
{
dummy = fgetc (stdin);
ungetc (dummy, stdin);
// Search for character
if (dummy == 0x03)
// Todo
}
}
return(0);
}
なぜ、プログラムがfgetcにスタックしているのかが間違っています。
私は多くの時間を節約するパイプではなく、標準入力からのターミナル入力を使用していることを指摘してくれてありがとう。 'のungetc()'パイプに文字をプッシュバックしません: –
あなたは関数がと言っていますか? – user3629249
いいえ、私は言っている[ 'ungetc'](https://linux.die.net/man/3/ungetc)であるFILE *' 'ストリーム上で動作libcの関数で、 - の上にlibcの抽象化実際のファイル記述子(あなたのパイプのようなもの) –