简体   繁体   English

此代码段的C#等价物是什么?

[英]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#. 我不太了解Perl,所以我想知道是否有人可以向我暗示一下此代码对STDIN的要求,以及如何在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. 该代码从STDIN(等于Console.In)读取所有内容,直到EOF读取到临时文件,然后将STDIN重定向到该临时文件。 In C#, you redirect with the Console.SetIn() method. 在C#中,使用Console.SetIn()方法重定向。

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. 如果STDIN未连接到普通文件,它将打开一个临时文件,从标准输入复制到该临时文件(最多EOF),然后连接STDIN以从该文件读取。 Presumably, it does that because later it wants to be able to seek on STDIN, and you can't seek on a special file. 大概是这样做的,因为稍后它希望能够在STDIN上进行seek ,而您不能在特殊文件上进行搜索。

-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). -f STDIN如果STDIN连接到普通文件,则-f STDIN为true;如果连接到特殊文件(如控制台或管道),则为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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