如何从另一个程序运行程序并通过c或c ++中的stdin将数据传递给它?

时间:2014-01-20 10:12:19

标签: c++ c stdin inter-process-communicat

说我有一个exe,让我们说sum.exe。现在说sum.exe的代码是

void main ()
{
 int a,b;
 scanf ("%d%d", &a, &b);
 printf ("%d", a+b);
}

我想知道如何从另一个c / c ++程序运行这个程序并通过stdin传递输入,就像在ideone这样的在线编译器站点中输入,我输入代码并在文本框中提供stdin数据和数据程序使用scanf或cin接受。另外,我想知道是否有任何方法可以从启动它的原始程序中读取该程序的输出。

6 个答案:

答案 0 :(得分:3)

在名称以X结尾的平台上的C(即非Windows)中,关键组件是:

  1. pipe - 返回一对文件描述符,以便可以从另一个中读取写入的文件描述符。

  2. fork - 将进程分为两个,两个都继续运行相同的代码。

  3. dup2 - 重新编号文件描述符。这样,您可以将管道的一端转换为stdin或stdout。

  4. exec - 在同一过程中停止运行当前程序,开始运行另一个程序。

  5. 将它们全部结合起来,你就能得到你所要求的东西。

答案 1 :(得分:3)

我知道这样做的最简单方法是使用popen()函数。它适用于Windows和UNIX。另一方面,popen()仅允许单向通信。

例如,要将信息传递给sum.exe(尽管您无法回读结果),您可以这样做:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f;

    f = popen ("sum.exe", "w");
    if (!f)
    {
        perror ("popen");
        exit(1);
    }

    printf ("Sending 3 and 4 to sum.exe...\n");
    fprintf (f, "%d\n%d\n", 3, 4);

    pclose (f);
    return 0;
}

答案 2 :(得分:1)

这是我的解决方案,它起作用了:

sum.cpp

#include "stdio.h"
int main (){
  int a,b;
  scanf ("%d%d", &a, &b);
  printf ("%d", a+b);
  return 0;
}

TEST.CPP

#include <stdio.h>
#include <stdlib.h>

int main(){
  system("./sum.exe < data.txt");
  return 0;
}

data.txt中

3 4

试试这个解决方案:)

答案 3 :(得分:0)

听起来你是来自Windows环境,所以这可能不是你想要的答案,但是从命令行你可以使用管道重定向操作符'|'将一个程序的stdout重定向到另一个程序的stdin。 http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

你可能最好在bash shell中工作,你可以在Windows上使用cygwin http://cygwin.com/

此外,您的示例看起来像是C ++和C的混合,而main的声明并不完全是公认的标准。

答案 4 :(得分:0)

如何做到这一点取决于平台。

在Windows下,使用CreatePipe和CreateProcess。您可以从MSDN中找到示例: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

在Linux / Unix下,您可以使用dup() / dup2()

一种简单的方法是使用终端(如Windows中的命令提示符)并使用|重定向输入/输出。

示例:

program1 | program2

这会将program1的输出重定向到program2的输入。 要检索/输入日期,您可以使用临时文件,如果您不想使用临时文件,则必须使用管道。

对于Windows,(使用命令提示符):

program1 <input >output 

对于Linux,您可以使用tee实用程序,通过在linux终端中键入man tee可以找到详细说明

答案 5 :(得分:0)

如何执行此操作(您必须检查错误,即 pipe()==-1dup()!=0 等,我在以下代码段中没有这样做)。

此代码运行您的程序“sum”,向其写入“2 3”,然后读取 sum 的输出。接下来,它将输出写入标准输出。

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>

int main() {

    int parent_to_child[2], child_to_parent[2];
    pipe(parent_to_child);
    pipe(child_to_parent);

    char name[] = "sum";
    char *args[] = {name, NULL};
    switch (fork()) {

    case 0:
        // replace stdin with reading from parent
        close(fileno(stdin));
        dup(parent_to_child[0]);
        close(parent_to_child[0]);

        // replace stdout with writing to parent
        close(fileno(stdout));
        dup(child_to_parent[1]);
        close(child_to_parent[1]);

        close(parent_to_child[1]); // dont write on this pipe
        close(child_to_parent[0]); // dont read from this pipe

        execvp("./sum", args);
        break;

    default:
        char msg[] = "2 3\n";

        close(parent_to_child[0]); // dont read from this pipe
        close(child_to_parent[1]); // dont write on this pipe

        write(parent_to_child[1], msg, sizeof(msg));
        close(parent_to_child[1]);

        char res[64];
        wait(0);
        read(child_to_parent[0], res, 64);
        printf("%s", res);
        exit(0);
    }
}

我正在按照@ugoren 在 their answer 中的建议进行操作:

  • 为进程间通信创建两个管道
  • 使用 dup 用管道的末端替换 stdin 和 stdout
  • 通过管道发送数据