简体   繁体   中英

what would be the c# equivalent of this code snippet?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM