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.
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.
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;
}
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
}
}
}
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.
Notes:
I have tried both scanf and fgets in StringGen, both produce the same result.
I have tried using a literal string with WriteLine(), but still no input.
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.
I am not overly familiar with C# as I am editing someone elses code to perform tests on StringGen.
See Question&Answers more detail:
os