open stdin a second time

A

amit khan

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

Ben Pfaff

amit khan said:
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.
 
K

Keith Thompson

amit khan said:
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.
 
S

Seebs

'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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top