控制台,标准输入,管道和readline。 [英] console, stdin, pipe and readline.

查看:117
本文介绍了控制台,标准输入,管道和readline。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试测试一个简单的控制台应用程序,如果你输入管道

输入就可以从标准输入中读取:

" c:\ type config.txt | consoleinput.exe"


似乎遇到了问题。看起来如果你使用烟斗,你可以阅读

所有的输入线,直到eof。但是,如果你需要提示输入

,那么管道仍然连接起来,因此Read或ReadLine在该点之后返回-1或

null。所以你不能提示按一个键继续,

等我猜我需要以某种方式断开管道与标准输入或

刷新" stdin流,所以它再次开始等待正常的控制台

输入。尝试了各种各样的东西,但似乎无法再次正常阅读

或Readline行为。有什么想法吗? TIA


==从Main调用的测试方法===

static void ReadStdIn()

{

字符串行;

ArrayList al = new ArrayList(200);

while((line = Console.ReadLine())!= null)

{

al.Add(line);

}


string s = Console.ReadLine();

if(s == null)

Console.WriteLine(" Input is null。");

else

Console.WriteLine("输入是:" + s);


}


-

William Stacey,MVP

Trying to test a simple console app that reads from standard in if you pipe
input into it like:
"c:\ type config.txt | consoleinput.exe"

Seem to run into an issue. It seems like if you use a pipe, you can read
all the input lines fine until eof. However, if you need to then prompt for
input, the pipe is still hooked up so Read or ReadLine returns with -1 or
null after that point. So you can''t prompt for "Hit a key to continue",
etc. I am guessing I need to somehow disconnect the pipe from stdin or
"refresh" the stdin stream so it again starts waiting for normal console
input. Have tried various things, but can''t seem to again get normal Read
or Readline behavior. Any thoughts? TIA

== Test Method called from Main ===
static void ReadStdIn()
{
string line;
ArrayList al = new ArrayList(200);
while ( (line = Console.ReadLine()) != null )
{
al.Add(line);
}

string s = Console.ReadLine();
if ( s == null )
Console.WriteLine("Input is null.");
else
Console.WriteLine("Input is:" + s);

}

--
William Stacey, MVP

推荐答案

Hi William,


我查看了你的帖子,我会花一些时间,我会尽快回复你




感谢您的理解。


祝你好运,

Jeffrey Tan

微软在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证也没有授予任何权利。

Hi William,

I have viewed your post, I will spend some time on it, I will reply to you
ASAP.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


感谢Jeffrey。这可能是正常行为。然而,像更少

之类的东西似乎只是在排空管道之后才会这样做,他们提示在屏幕显示后按任意键

。他们可能正在做一些其他的伎俩,比如新的

流程等。非常感谢您的帮助。干杯!


-

William Stacey,MVP


"" Jeffrey Tan [MSFT]" ;" <,V - ***** @ online.microsoft.com>在消息中写道

news:b9 ************** @ cpmsftngxa10.phx.gbl ...
Thanks Jeffrey. This may be normal behavior. However, things like "less"
seem to do just this after draining the pipe they prompt to hit any key
after a screenfull. They probably are doing some other trick like a new
process or something. Your help is appreciated. Cheers!

--
William Stacey, MVP

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:b9**************@cpmsftngxa10.phx.gbl...
Hi William,

我查看过您的帖子,我会花一些时间,我会尽快回复您。

感谢您的理解。
Jeffrey Tan
微软在线合作伙伴支持
获得安全! - www.microsoft.com/security
此帖子提供就像没有保证,也没有授予任何权利。
Hi William,

I have viewed your post, I will spend some time on it, I will reply to you
ASAP.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.






Hi William,


到目前为止我我想,我们应该可以使用Console.OpenStandardInput方法来获取标准输入并设置回来。像这样:

Stream st = Console.OpenStandardInput();

StreamReader sr = new StreamReader(st);

控制台.SetIn(sr);


但是,我无法按预期完成这项工作。无论如何,我仍然会花一些时间在它上面。请稍等一下。


感谢您的理解。


祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。

Hi William,

So far as I think, we should can use Console.OpenStandardInput method to
get the standard input and set back. Like this:

Stream st=Console.OpenStandardInput ();
StreamReader sr=new StreamReader(st);
Console.SetIn(sr);

But, I can not get this work as expected. Anyway, I will still spend some
time on it. Please wait a little more time.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


这篇关于控制台,标准输入,管道和readline。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆