I want to search a character in stream and push back in stream without consuming any data. I am using fgetc but program is stuck on callling fgetc.
below is my test program.
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);
}
So, what is wrong with code why program stuck on fgetc.