从 C# 中的同一个流读取多次

发布于 2024-07-20 11:18:41 字数 556 浏览 2 评论 0原文

我想多次读取程序的输出。 比如,如果我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-07-27 11:18:41

如果流支持查找 (CanSeek),您可以通过设置“倒带”它,

stream.Position = 0;

从而开始重新读取它。

If the stream supports seeking (CanSeek), you can "rewind" it by setting

stream.Position = 0;

thus starting to read it all over again.

ぃ弥猫深巷。 2024-07-27 11:18:41

如果流不支持查找,但流中的数据不太大,则可以将该流读写到 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.

暖树树初阳… 2024-07-27 11:18:41

Quck and Dirty:这个有一些小故障。 尝试改进它,因为我要离开办公室:)

        ProcessStartInfo psi = new ProcessStartInfo(@"c:\temp\testC.exe");
        psi.CreateNoWindow = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;

        Process p = Process.Start(psi);
        string input = "";

        ConsoleColor fc = Console.ForegroundColor;

        StreamWriter sw = p.StandardInput;
        StreamReader sr = p.StandardOutput;

        char[] buffer = new char[1024];
        int l = 0;

        do
        {
            Console.Write("Enter input: ");
            input = Console.ReadLine();

            int i = Convert.ToInt32(input);

            sw.Write(i);
            sw.Write(sw.NewLine);

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.Write(">> ");

            l = sr.Read(buffer, 0, buffer.Length);

            for (int n = 0; n < l; n++)
                Console.Write(buffer[n] + " ");

            Console.WriteLine();

            Console.ForegroundColor = fc;
        } while (input != "10");

        Console.WriteLine("Excution Finished. Press Enter to close.");
        Console.ReadLine();
        p.Close();

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

        ProcessStartInfo psi = new ProcessStartInfo(@"c:\temp\testC.exe");
        psi.CreateNoWindow = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.UseShellExecute = false;

        Process p = Process.Start(psi);
        string input = "";

        ConsoleColor fc = Console.ForegroundColor;

        StreamWriter sw = p.StandardInput;
        StreamReader sr = p.StandardOutput;

        char[] buffer = new char[1024];
        int l = 0;

        do
        {
            Console.Write("Enter input: ");
            input = Console.ReadLine();

            int i = Convert.ToInt32(input);

            sw.Write(i);
            sw.Write(sw.NewLine);

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.Write(">> ");

            l = sr.Read(buffer, 0, buffer.Length);

            for (int n = 0; n < l; n++)
                Console.Write(buffer[n] + " ");

            Console.WriteLine();

            Console.ForegroundColor = fc;
        } while (input != "10");

        Console.WriteLine("Excution Finished. Press Enter to close.");
        Console.ReadLine();
        p.Close();

PS:- I've created console exe in vs2008 and copied it to c:\temp folder under name testC.exe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文