open stdin a second time

Discussion in 'C Programming' started by amit khan, May 25, 2010.

  1. amit khan

    amit khan Guest

    Hi,

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

    r=popen("/usr/local/bin/mfilt","w");
    while((ret=fread(l,1,sizeof(l),stdin))>0){
    fwrite(l,1,sizeof(l),r)
    }
    pclose(r);

    After that, if mfilt 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 ?
    amit khan, May 25, 2010
    #1
    1. Advertising

  2. amit khan

    Ben Pfaff Guest

    amit khan <> writes:

    > I´m opening stdin to get a file and pass it through a pipe.
    >
    > r=popen("/usr/local/bin/mfilt","w");
    > while((ret=fread(l,1,sizeof(l),stdin))>0){
    > fwrite(l,1,sizeof(l),r)
    > }
    > pclose(r);
    >
    > After that, if mfilt 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 ?


    No, there is no way to do that. You will have to save the
    message or convince the "mfilt" program to save a copy for you if
    it fails.
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, May 25, 2010
    #2
    1. Advertising

  3. amit khan <> writes:
    > I´m opening stdin to get a file and pass it through a pipe.
    >
    > r=popen("/usr/local/bin/mfilt","w");
    > while((ret=fread(l,1,sizeof(l),stdin))>0){
    > fwrite(l,1,sizeof(l),r)
    > }
    > pclose(r);


    Yourspacebarappearstobebroken.

    Incidentally, popen() isn't a standard C function, but that doesn't
    seem to be relevant to your question.

    'l' (lowercase L) is a really bad name; it's almost indistinguishable
    from '1' (digit one). Showing us the declaration would have been
    helpful. Your code makes sense only if l is an array object; is it?

    In any case, the code you posted isn't the code you compiled.
    If it were, your compiler would have caught the missing semicolon.
    "Doctor, it hurts when I do something that vaguely resembles this,
    but that differs in ways I'm not going to bother to tell you about."

    fread() is usually used to read binary data. Applying it to stdin,
    which is a text stream, is unusual. It's not illegal, but if you're
    reading fixed-length records you probably don't want line endings
    to be converted.

    The way you've written the fread() call, it can read a partial
    record; if it does, you still write the entire record, probably
    re-writing the tail end of the previous record.

    > After that, if mfilt 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.


    There's no reliable way to do that. Consider the common case
    where stdin reads from a keyboard. Do you want to ask the user to
    re-type everything?

    You can try fseek(stdin, 0, SEEK_SET), but be sure to check whether it
    succeeded, and be prepared to cope with failure.

    You haven't given us enough context to be sure, but I strongly suspect
    it would make more sense to pass the name of your input file to the
    program, and let the program open the file itself (probably in binary
    mode).

    > 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 ?


    Save the message to a temp file or to memory. Or rethink your design.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 25, 2010
    #3
  4. amit khan

    Seebs Guest

    On 2010-05-25, Keith Thompson <> wrote:
    > amit khan <> writes:


    >> while((ret=fread(l,1,sizeof(l),stdin))>0){
    >> fwrite(l,1,sizeof(l),r)


    > 'l' (lowercase L) is a really bad name; it's almost indistinguishable
    > from '1' (digit one). Showing us the declaration would have been
    > helpful. Your code makes sense only if l is an array object; is it?


    If this isn't the same troll as "sandeep", I'm gonna be very surprised.
    Same fake address, both ask questions which are carefully laced with
    layers upon layers of bad ideas.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, May 25, 2010
    #4
    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:
    24,770
    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:
    755
    Dan Pop
    Jun 21, 2004
  3. fbertasso

    open stdin a second time

    fbertasso, Jun 9, 2008, in forum: C Programming
    Replies:
    12
    Views:
    638
    Walter Roberson
    Jun 24, 2008
  4. Ben
    Replies:
    2
    Views:
    1,314
    jacob navia
    Aug 29, 2009
  5. FBert

    open stdin a second time

    FBert, Nov 11, 2011, in forum: C Programming
    Replies:
    2
    Views:
    356
    Eric Sosman
    Nov 12, 2011
Loading...

Share This Page