从 C# 中的同一个流读取多次
我想多次读取程序的输出。 比如,如果我通过 X,我会得到输出并显示它,然后,如果我通过 Y,我会得到输出并显示它。 无需重新启动该进程。 为了尝试它,我制作了 ac 程序
#include<stdio.h>
int main()
{
int i;
int j;
while(scanf("%d", &i))
{
for(j = 0; j<=i;j++)
printf("%d\n",j);
}
return 0;
}
,现在我将它与 C# 进行交互,当我在文本框中输入文本时,它会通过重定向标准输入(流写入器)传递到程序并读取输出,我称之为标准输出(流阅读器).readtoend()。
但这对我不起作用。 当它进入等待状态时,直到流返回一些指示,告诉结束已被读取。
我怎样才能实现这样的目标?
我也尝试了异步读取,我调用了 beginoutputread 方法,但是我不知道读取何时完成! 一种方法是我在原始程序中添加一个标记,以指示当前输入的输出已结束。 我还有其他方法可以实现吗?
i want to read the output of my program multiple times. Some thing like if i pass X i get the output and i display it, then again if i pass Y i get the output and i display it. without restarting the process.
to try it i have made a c program
#include<stdio.h>
int main()
{
int i;
int j;
while(scanf("%d", &i))
{
for(j = 0; j<=i;j++)
printf("%d\n",j);
}
return 0;
}
and now i'm interfacting it with C#, where when i enter a text in the textbox it is passed through the redirect standardinput (a streamwriter) to the program and to read the output i call it's standardoutput (a streamreader).readtoend().
But it is not working for me. As it goes in waitstate till the stream returns some indication telling end has been read.
How can i achieve such a thing?
I tried the asynchronous read too where i call the beginoutputread method, but then i won't know when the read has been finished! One way can be for me to add a marker in my original program to indicate that output is over for the current input. Is there any other way for me to achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果流支持查找 (
CanSeek
),您可以通过设置“倒带”它,从而开始重新读取它。
If the stream supports seeking (
CanSeek
), you can "rewind" it by settingthus starting to read it all over again.
如果流不支持查找,但流中的数据不太大,则可以将该流读写到 MemoryStream,并根据需要多次从 MemoryStream 中读取。
If the stream does not support seeking but the data in stream is not so big, you can read and write that stream to MemoryStream, and read from MemoryStream as many times as you want.
Quck and Dirty:这个有一些小故障。 尝试改进它,因为我要离开办公室:)
PS:- 我在 vs2008 中创建了控制台 exe,并将其复制到名为 testC.exe 的 c:\temp 文件夹中。
Quck and Dirty: This one works with some minor glitches. Try improving it, because I'm leaving office :)
PS:- I've created console exe in vs2008 and copied it to c:\temp folder under name testC.exe.