C# 在c语言中多次读取同一数据流#
我想多次读取程序的输出。例如,如果我通过X,我得到输出并显示它,然后如果我通过Y,我得到输出并显示它。无需重新启动进程。 为了试一下,我做了一个c程序C# 在c语言中多次读取同一数据流#,c#,process,C#,Process,我想多次读取程序的输出。例如,如果我通过X,我得到输出并显示它,然后如果我通过Y,我得到输出并显示它。无需重新启动进程。 为了试一下,我做了一个c程序 #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; } #包括 int main() { int
#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;
}
#包括
int main()
{
int i;
int j;
while(scanf(“%d”、&i))
{
对于(j=0;j如果流支持查找(CanSeek
),您可以通过设置
stream.Position = 0;
因此,我开始重新阅读它。怪模怪样:这本书存在一些小问题。尝试改进一下,因为我要离开办公室了:)
ProcessStartInfo psi=newprocessstartinfo(@“c:\temp\testC.exe”);
psi.CreateNoWindow=真;
psi.error=true;
psi.INPUT=真;
psi.0输出=真;
psi.UseShellExecute=false;
过程p=过程启动(psi);
字符串输入=”;
ConsoleColor fc=Console.ForegroundColor;
StreamWriter sw=p.StandardInput;
StreamReader sr=p.StandardOutput;
char[]buffer=新字符[1024];
int l=0;
做
{
控制台。写入(“输入:”);
input=Console.ReadLine();
int i=转换为32(输入);
sw.Write(i);
sw.Write(sw.NewLine);
Console.ForegroundColor=ConsoleColor.Yellow;
控制台。写入(“>>”;
l=sr.Read(缓冲区,0,缓冲区长度);
对于(int n=0;n
PS:-我在vs2008中创建了console exe,并将其复制到名为testC.exe的c:\temp文件夹中。如果流不支持查找,但流中的数据不是很大,您可以将该流读写到MemoryStream,并从MemoryStream中读取任意次数。流不支持查找,:(你的最后两点似乎很矛盾。你想等阅读结束吗?
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();