Anirudh Goel
Anirudh Goel

Reputation: 4721

reading multiple times from same stream in c#

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?

Upvotes: 3

Views: 14836

Answers (3)

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

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.

Upvotes: 5

TheVillageIdiot
TheVillageIdiot

Reputation: 40517

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.

Upvotes: 0

DreamSonic
DreamSonic

Reputation: 1472

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

stream.Position = 0;

thus starting to read it all over again.

Upvotes: 12

Related Questions