open stdin a second time

Discussion in 'C Programming' started by FBert, Nov 11, 2011.

  1. FBert

    FBert Guest

    Hi,

    I´m opening stdin to get a file and pass it through a pipe.

    razor=popen("/var/qmail/bin/razor-check -home=/var/qmail/razor",
    "w");
    while((ret=fread(linha,1,sizeof(linha),stdin))>0)
    {fwrite(linha,1,sizeof(linha),razor);}
    pclose(razor);

    After that, if razor returns me an error I want to send an email with
    the original message attached...but how can I re-read stdin to do
    that.

    I do not want to save the message to a temp file or save it to memory
    for i/o questions. Is there a way to re-read stdin ?
     
    FBert, Nov 11, 2011
    #1
    1. Advertising

  2. FBert

    James Kuyper Guest

    On 11/11/2011 03:11 PM, FBert wrote:
    > Hi,
    >
    > I´m opening stdin to get a file and pass it through a pipe.
    >
    > razor=popen("/var/qmail/bin/razor-check -home=/var/qmail/razor",
    > "w");
    > while((ret=fread(linha,1,sizeof(linha),stdin))>0)
    > {fwrite(linha,1,sizeof(linha),razor);}
    > pclose(razor);
    >
    > After that, if razor returns me an error I want to send an email with
    > the original message attached...but how can I re-read stdin to do
    > that.
    >
    > I do not want to save the message to a temp file or save it to memory
    > for i/o questions. Is there a way to re-read stdin ?


    That depends upon what kind stream stdin is. The message has to be
    stored somewhere if you're to retrieve it. If stdin is a terminal, for
    instance, if you don't save the message yourself, then it isn't saved
    anywhere, and you're out of luck.

    On the other hand, if stdin is a file, the information is saved - in
    that file. You can use rewind() to go back to the beginning of the file,
    or either fseek() or fsetpos() to go back to specific saved positions in
    the file.
     
    James Kuyper, Nov 11, 2011
    #2
    1. Advertising

  3. FBert

    Eric Sosman Guest

    On 11/11/2011 3:11 PM, FBert wrote:
    > Hi,
    >
    > I´m opening stdin to get a file and pass it through a pipe.
    >
    > razor=popen("/var/qmail/bin/razor-check -home=/var/qmail/razor",
    > "w");
    > while((ret=fread(linha,1,sizeof(linha),stdin))>0)
    > {fwrite(linha,1,sizeof(linha),razor);}
    > pclose(razor);
    >[...]


    See James Kuyper's response. In addition, note that the third
    argument to fwrite() should almost certainly be `ret', because there's
    an excellent chance that the tail end of `linha' will be whatever was
    left over from the next-to-last input line. (For extra credit, examine
    the value returned by fwrite() to see whether it actually worked.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Nov 12, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,023
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    793
    Dan Pop
    Jun 21, 2004
  3. fbertasso

    open stdin a second time

    fbertasso, Jun 9, 2008, in forum: C Programming
    Replies:
    12
    Views:
    650
    Walter Roberson
    Jun 24, 2008
  4. Ben
    Replies:
    2
    Views:
    1,349
    jacob navia
    Aug 29, 2009
  5. amit khan

    open stdin a second time

    amit khan, May 25, 2010, in forum: C Programming
    Replies:
    3
    Views:
    1,371
    Seebs
    May 25, 2010
Loading...

Share This Page