I don't really understand Perl, so I was wondering if someone could give me a hint about what it is this code is asking of STDIN, and how to say this in C#. Thanks.
$TMPFILE = "xxx.tmp";
if 开发者_开发百科(! -f STDIN) {
open TMPFILE, "> $TMPFILE"
or die "Couldn't open `$TMPFILE' for writing: $!; aborting";
print TMPFILE while <STDIN>;
close TMPFILE;
open STDIN, "< $TMPFILE"
or die "Couldn't open `$TMPFILE' for reading: $!; aborting";
unlink $TMPFILE;
}
The code reads everything from STDIN (equals Console.In) until EOF to a temp file, and then redirects STDIN to that temp file. In C#, you redirect with the Console.SetIn() method.
If STDIN is not connected to an ordinary file, it opens a temporary file, copies from standard input to the temporary file (up to EOF), and then connects STDIN to read from that file. Presumably, it does that because later it wants to be able to seek
on STDIN, and you can't seek on a special file.
-f STDIN
is true if STDIN is connected to an ordinary file, and false if it's connected to a special file (like the console or a pipe).
精彩评论