【发布时间】:2017-01-31 12:46:34
【问题描述】:
我想在流中搜索一个字符并在流中推回而不消耗任何数据。 我正在使用 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 上的代码有什么问题。
【问题讨论】: