【问题标题】:Multiple Inputs to a C Program through C#通过 C# 对 C 程序的多个输入
【发布时间】:2014-08-28 07:27:54
【问题描述】:

我正在将输入传递给通过 C# 由 Compiler 编译 C 程序生成的 .exe 文件。我的 C# 代码是:

string line = "1" + " 2 " ;

Process p = new Process();                
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "C:\\..\\demo1.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(line);

p.Close();

我成功地为读取一个输入的 C 程序传递了单个输入。但是当我尝试将多个输入传递给需要多个输入的 C 程序时,它只运行 exe 文件并且它不读取/传递我提供的任何输入。我也隐藏了exe文件的进程,但它会打开它。

我尝试向其传递多个输入的 C 代码是:

#include<stdio.h>

int main()
{
   int a, b, c;

   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);

   c = a + b;

   printf("Sum of entered numbers = %d\n",c);

   return 0;
}

我们将不胜感激任何形式的帮助/建议。

【问题讨论】:

    标签: c# c++ c


    【解决方案1】:

    您是否尝试过使用Write 而不是WriteLine

    另外,如果可能的话,我建议更改 C 应用程序以将您的输入作为命令行参数,这样您就可以直接使用输入启动它。

    【讨论】:

    • 使用 Write 代替 WriteLine 不会打开 exe 并在隐藏状态下运行它,但它会将数字“1”和“2”的总和显示为数字“12”。
    • 我正在开发这个项目以使用与 C# 应用程序和编译器链接的网站在线检查 C 编程作业,因此将代码更改为命令行参数与要求相矛盾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多