windows 将一个 C 程序的输出获取到另一个 C 程序中的变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8723628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:47:21  来源:igfitidea点击:

Get the output of one C program into a variable in another C program

c++cwindowsvariables

提问by Ronin

I have 2 C programs. Say one is program-1.c

我有 2 个 C 程序。说一个是program-1.c

int main(){
printf("hello world");
}

Now in 2nd code named program-2.c, I want the output of 1st code into a variable, so that I can have the output "hello world" into a variable in the 2nd C code.

现在在名为 的第二个代码中program-2.c,我希望将第一个代码的输出转换为变量,以便我可以将输出“hello world”转换为第二个 C 代码中的变量。

How can I do this?

我怎样才能做到这一点?

回答by Mat

You can use the popenfunction for this:

您可以popen为此使用该功能:

FILE* proc1 = popen("./program1", "r");
// Usual error handling code goes here
// use the usual FILE* read functions
pclose(proc1);

回答by Alok Save

You will need to run the two programs in two separate processes and then use some sort of IPC mechanism to exchange data between the two processes.

您需要在两个单独的进程中运行这两个程序,然后使用某种 IPC 机制在两个进程之间交换数据。

回答by Bo Persson

On many operating systems you can get the output from one console program as input to the next, perhaps

在许多操作系统上,您可以将一个控制台程序的输出作为下一个程序的输入,也许

program-1 > program-2

you can then read the result from standard input

然后您可以从标准输入读取结果

std::string  variable;

std::getline(std::cin, variable);

回答by Vinay Jain

Sample Code for "Output of one program is input of another program Using Pipes"

“一个程序的输出是另一个程序使用管道的输入”的示例代码

#include <unistd.h>
#include <process.h>

/* Pipe the output of program to the input of another. */

int main()
{
  int pipe_fds[2];
  int stdin_save, stdout_save;

  if (pipe(pipe_fds) < 0)
    return -1;

  /* Duplicate stdin and stdout so we can restore them later. */
  stdin_save = dup(STDIN_FILENO);
  stdout_save = dup(STDOUT_FILENO);

  /* Make the write end of the pipe stdout. */
  dup2(pipe_fds[1], STDOUT_FILENO);

  /* Run the program. Its output will be written to the pipe. */
  spawnl(P_WAIT, "/dev/env/DJDIR/bin/ls.exe", "ls.exe", NULL);

  /* Close the write end of the pipe. */
  close(pipe_fds[1]);

  /* Restore stdout. */
  dup2(stdout_save, STDOUT_FILENO);

  /* Make the read end of the pipe stdin. */
  dup2(pipe_fds[0], STDIN_FILENO);

  /* Run another program. Its input will come from the output of the
     first program. */
  spawnl(P_WAIT, "/dev/env/DJDIR/bin/less.exe", "less.exe", "-E", NULL);

  /* Close the read end of the pipe. */
  close(pipe_fds[0]);

  /* Restore stdin. */
  dup2(stdin_save, STDIN_FILENO);

  return 0;
}

Cheers....

干杯....

回答by Vinay Jain

On windows u can use this example...

在 Windows 上你可以使用这个例子......

#include <iostream>
#include<time.h>
?
using namespace std;
?
int main()
{
    int a=34, b=40;
?
    while(1)
    {
        usleep(300000);?  
        cout << a << " " << b << endl;
    }
}



#include<iostream>
?
using namespace std;
?
int main()
{
    int a, b;
?
    while(1)
    {
    cin.clear();
?
        cin >> a >> b;
?
        if (!cin) continue;
?
        cout << a << " " << b << endl;
    }
}

You have to observe and set the usleep() value to successfully get the input from the output of the other program. Run both programs simultaneously. Enjoy..:)

您必须观察并设置 usleep() 值才能成功从其他程序的输出中获取输入。同时运行两个程序。享受..:)

回答by user1129769

In the code for the program-2.c you should use int argcand char *argv[]to get the output from program-1.c

在 program-2.c 的代码中,您应该使用int argcchar *argv[]从 program-1.c 获取输出

So program-2.c should look like this:

所以 program-2.c 应该是这样的:

void main(int argc, char *argv[]) 
{
   int i;

   for( i=0; i<argc; i++ ) 
   {
        printf("%s", argv[i]); //Do whatever you want with argv[i]
   }       

}

Then in the command prompt program-1 > program-2

然后在命令提示符 program-1 > program-2