Re: Need to block on file eof read

From: Andrei Voropaev (avorop_at_mail.ru)
Date: 12/28/04

  • Next message: Chuck Dillon: "Re: Need to block on file eof read"
    Date: 28 Dec 2004 17:58:54 GMT
    
    

    On 2004-12-28, James Johnson <jj@yaaho.com> wrote:
    >
    >
    > I need to do something like this, but get the read (fgets in this case)
    > to block until there is a new line of data. Sort of like tail -f.
    >
    > FILE *instream = fopen(fname, "rt");
    > if (instream==NULL) {
    > return 0;
    > }
    > fseek(instream, 0L, SEEK_END);
    >
    > while(1) {
    > cptr = fgets(buf, sizeof(buf), instream);
    > if (cptr==NULL) {
    > printf("exiting because there is no data\n");
    > break;
    > }
    > printf(cptr);
    > }
    >
    > fclose(instream);
    >
    >
    > How can this be done in unix?

    On linux, normally one calls fgets, if NULL then sleeps for a while, and
    after that tries again. Check the strace of tail -f, you'll see it.

    I don't believe (though I don't know it ;) that there's any other way.

    Andrei


  • Next message: Chuck Dillon: "Re: Need to block on file eof read"

    Relevant Pages

    • Re: Need to block on file eof read
      ... James Johnson wrote: ... > I need to do something like this, but get the read (fgets in this case) ... Sort of like tail -f. ... NimbleGen Systems Inc. ...
      (comp.unix.programmer)
    • Re: Need to block on file eof read
      ... >> I need to do something like this, but get the read (fgets in this case) ... >tail does you have to periodically use stat/fstat to check the length ... That "sort of" works the same as tail -f. ... Jim ...
      (comp.unix.programmer)
    • Re: Need to block on file eof read
      ... >> I need to do something like this, but get the read (fgets in this case) ... >tail will poll for new data when it reaches the end of the file. ... >On some unix, you can use kqueue or the linux equivalent to avoid ... Jim ...
      (comp.unix.programmer)
    • Re: Need to block on file eof read
      ... James Johnson wrote: ... >I need to do something like this, but get the read (fgets in this case) ... Consider this snippet: ... fgets, instream); ...
      (comp.unix.programmer)