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.