C# 为什么Process.StandardInput.WriteLine不向我的流程提供输入?

C# 为什么Process.StandardInput.WriteLine不向我的流程提供输入?,c#,c,input,process,C#,C,Input,Process,这是我的第一个问题,所以我会尽可能详细地回答。我目前正在开发一个C#程序(我们称之为TestProgram),它测试用C编写的不同程序(我称之为StringGen)。TestProgram应该在命令窗口中运行StringGen,然后向它提供一组输入字符串,并记录每个字符串的输出。当StringGen运行时,它启动一个while循环,等待输入,将该输入提交给处理函数,然后返回结果 我的问题来自于我试图让TestProgram向StringGen提交一个字符串。我将StringGen作为一个进程启动

这是我的第一个问题,所以我会尽可能详细地回答。我目前正在开发一个C#程序(我们称之为TestProgram),它测试用C编写的不同程序(我称之为StringGen)。TestProgram应该在命令窗口中运行StringGen,然后向它提供一组输入字符串,并记录每个字符串的输出。当StringGen运行时,它启动一个while循环,等待输入,将该输入提交给处理函数,然后返回结果

我的问题来自于我试图让TestProgram向StringGen提交一个字符串。我将StringGen作为一个进程启动,并尝试使用process.StandardInput.WriteLine()为其输入,然后使用process.StandardOutput.ReadLine()查找输出。在我进一步阐述之前,我将提供一些代码

以下是StringGen的主要功能:

int main() {
    char result[255];
    char input[255];

    do {
        fgets(input, 100, stdin);
        result = GetDevices(input); //Returns the string required
        printf("%s", result);
    } while (input != "quit");

    return 0;
}
以下是我将StringGen定义为流程的C代码:

Process cmd = new Process();

ProcessStartInfo info = new ProcessStartInfo(command, arguements); // Command is the path to the C executeable for StringGen
info.WorkingDirectory = workingDirectory; // Working Directory is the directory where Command is stored
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
cmd.StartInfo = info;
cmd.Start();
然后我继续使用这个过程:

using (var cmd)
        {
            // Loop through the input strings
            String response;

            foreach (exampleString in StringSet) // Loops through each string
            {
                cmd.StandardInput.WriteLine(exampleString.text); // This is the problem line
                response = cmd.StandardOutput.ReadLine(); // System comes to a halt here
                cmd.StandardOutput.Close();
                if (response == "Something")
                {
                     // Do this
                }
                else
                {
                     // Do that
                }
            }
       }
WriteLine命令似乎没有向StringGen提供任何输入,因此系统挂起在ReadLine,因为StringGen没有返回任何输出。我尝试在命令行运行StringGen,它工作正常,从键盘获取输入并输出正确的字符串。我已经尝试了我能想到的一切,搜索了整个网站,还有其他人试图找到一个解决方案,但这种代码的每一个例子似乎都适合其他人。我看不出我做错了什么。如果有人能建议我从TestProgram向我的StringGen程序提交输入,我将非常感激。如果我遗漏了任何重要的内容,或者有任何不清楚的地方,请告诉我

注: 我在StringGen中尝试了scanf和FGET,两者都产生了相同的结果

我尝试将文字字符串与WriteLine()一起使用,但仍然没有输入

我曾尝试在TestProgram中使用Write()和Flush(),但没有效果

我试图关闭()输入缓冲区以强制刷新,但这也没有效果


我对C不太熟悉,因为我正在编辑其他人的代码以在StringGen上执行测试。

我认为问题出在你的C程序中,而不是你的C程序中。生成输出时,不会将
\n
放在末尾。因此
StandardOutput.ReadLine()
将永远等待,因为流中没有行尾标记

由于C程序的输出用于同步合作程序的步骤,因此在等待下一部分输入之前,最好将其刷新到输出:

printf("%s\n", result);
fflush(stdout);
你的C代码似乎还不错,我用另一个C程序试过了:

下面的代码输出“我收到了HelloWorld!”。所以问题一定出在你的C代码中。正如dasblinkenlight已经提到的,可能缺少新行符号

static void Main(string[] args)
{
    Process cmd = new Process();

    ProcessStartInfo info = new ProcessStartInfo(@"AnotherApp.exe", "");
    info.WorkingDirectory = @"path\to\folder";
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;
    info.UseShellExecute = false;
    cmd.StartInfo = info;
    cmd.Start();

    cmd.StandardInput.WriteLine("Hello world!");

    String output = cmd.StandardOutput.ReadLine();
    Console.WriteLine(output);

    Console.ReadKey();
}

哇,把它钉在一个。我做了您建议的更改,代码立即生效。我想我看的方向完全错了。非常感谢。当在输出流中检测到EndOfStream(或流已关闭)时,ReadLine不会返回吗,无论它是否遇到新行?如果稍后使用此代码遇到问题,请务必阅读不同流的同步重定向。在程序偶尔挂起的情况下,可能会遇到锁定问题。
static void Main(string[] args)
{
    Process cmd = new Process();

    ProcessStartInfo info = new ProcessStartInfo(@"AnotherApp.exe", "");
    info.WorkingDirectory = @"path\to\folder";
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.RedirectStandardError = true;
    info.UseShellExecute = false;
    cmd.StartInfo = info;
    cmd.Start();

    cmd.StandardInput.WriteLine("Hello world!");

    String output = cmd.StandardOutput.ReadLine();
    Console.WriteLine(output);

    Console.ReadKey();
}