本文介绍了为什么Process.StandardInput.WriteLine不向我的进程提供输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is my first question here so I'll be as detailed as I can. I am currently working on a C# program (We'll call it TestProgram) that tests a different program written in C (which I'll refer to as StringGen). TestProgram is supposed to run StringGen in a command window, then feed it a set of input strings and record the output for each one. When StringGen is run, it starts a while loop which waits for an input, submits that input to the processing function, then returns the result.

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

My problem comes from when I try and have TestProgram submit a string to StringGen. I'm starting StringGen as a process and attempting to feed it input with Process.StandardInput.WriteLine(), then looking for output with Process.StandardOutput.ReadLine(). Before I elaborate further, I'll supply some code.

这是StringGen的主要功能:

Here is the main function for 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#代码:

Here is the C# code where I define StringGen as a process:

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();

然后我继续使用此过程:

Then I go on to use this process as so:

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提供任何输入,因此由于StringGen没有提供任何输出,因此系统挂起在ReadLine.我试过在命令行上运行StringGen,它工作正常,并从键盘输入并输出正确的字符串.我已经尝试了所有可以想到的方法,并在该站点以及其他试图找到解决方案的站点中进行了搜索,但是这种代码的每个示例似乎对其他所有人都适用.我看不到我在做什么错.如果有人可以建议我可以从TestProgram向我的StringGen程序提交输入的方法,我将不胜感激.如果我遗漏了重要的内容或不清楚的地方,请告诉我.

The WriteLine command does not seem to give any input to StringGen, and so the system hangs at ReadLine because StringGen is not giving any output back. I've tried running StringGen at command line and it works fine and takes input from keyboard and outputs the correct strings back. I've tried everything I can think of and searched all over this site and others trying to find a solution but every example of this kind of code seems to be work fine for everyone else. I can't see what I'm doing wrong. If anyone could suggest a way that I can submit input to my StringGen program from TestProgram I would be really grateful. If I have left anything important out or if anything is unclear, please let me know.

注意:我已经在StringGen中尝试了scanf和fgets,它们都产生相同的结果.

Notes:I have tried both scanf and fgets in StringGen, both produce the same result.

我尝试过在WriteLine()中使用文字字符串,但仍然没有输入.

I have tried using a literal string with WriteLine(), but still no input.

我曾尝试在TestProgram中使用Write()和Flush(),但无济于事.

I have tried useing Write() and Flush() in TestProgram but to no avail.

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

I have tried to Close() the input buffer to force a flush, but this has no effect either.

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

I am not overly familiar with C# as I am editing someone elses code to perform tests on StringGen.

推荐答案

我认为问题出在您的C程序中,而不是您的C#程序中.产生输出时,请不要在最后放置\n.因此StandardOutput.ReadLine()将永远等待,因为流中没有行尾标记.

I think the problem is in your C program, not in your C# program. When you produce your output, you do not put \n at the end. Hence StandardOutput.ReadLine() will wait forever, because there is no end-of-line marker in the stream.

由于C程序的输出用于同步协作程序的步骤,因此在等待下一部分输入之前将其刷新到输出也是一个很好的主意:

Since the output of your C program is used to synchronize the steps of your co-operating programs, it is also a very good idea to flush it to the output before waiting for the next portion of input:

printf("%s\n", result);
fflush(stdout);

这篇关于为什么Process.StandardInput.WriteLine不向我的进程提供输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:38