Re: more pipe questions



On Thu, Feb 15, 2001 at 03:29:41PM +1100, Helmethead wrote:
> On Thu, Feb 15, 2001 at 02:53:02AM +0100, Ronald Bultje wrote:
> > I remember this question being asked before, but deleted it, I think...
> > 
> > If I write data to a file (continuously, a logfile kinda thing), how can I
> > check this file for "being updated"? I now use a read-pipe and popen("cat
> > file", "r"); but there's probably a better way.
> > 
> > Or is it maybe possible to open this logfile "continuously" so that any
> > data written to it can be caught at once?
> > 
> > Besides that, if I put this popen("cat file", "r"); in a while-loop, my
> > tasklist shows a [cat <defunc>] and the program stops there...... Any clue
> > why?
> > 
> 
> Why it's getting zombies (defunct processes) would be because you are not calling pclose() on each file handle. And if you want to use popen to do this, call tail -0f not cat. any updates to the file will be immediately printed with that command.
> 
> eg.
> 
> FILE *fp;
> if (!(fp = popen ("tail -0f filename", "r")))
> {
>     perror ("popen() failed");
>     exit (1);
> }
> printf ("Watching filename for new data\n\n");
> for (;;) /* infinite loop */
> {
>     char buff[80];
>     if (fgets (buff, 79, fp) == NULL)
>     {
> 	if (feof (fp))
> 	    fprintf (stderr, "tail closed\n");
> 	if (ferror (fp))
> 	    perror ("fgets() failed");

oops.. note to self, practice what preach.

	pclose (fp);

this is especially important if you aren't gonna exit straight away like this, I assume you won't

> 	exit (1);
>     }
>     printf ("line appended to file filename: %s\n", buff);
> }
> 
> yeah, gtk-ize all that.
> 
> > Ronald
> > 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]