【问题标题】:Get the output of one C program into a variable in another C program获取一个 C 程序的输出到另一个 C 程序的变量中
【发布时间】:2023-03-09 00:44:01
【问题描述】:

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

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

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

我该怎么做?

【问题讨论】:

  • 您实际上是指在两个单独的可执行文件(program-1.exe 和 program-2.exe)之间传递数据,还是在单独的代码文件中的两个类或函数之间传递数据,这就是您所展示的远吗?
  • 我需要两者 - Windows n linux...单独的解决方案。你能帮忙吗?
  • @ChrisBD :: 实际上我需要知道这两种情况....

标签: c++ c windows variables


【解决方案1】:

您可以为此使用popen 函数:

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

【讨论】:

【解决方案2】:

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

【讨论】:

  • +1 因为 IPC 几乎总是更好且可扩展的解决方案,除非这两个程序纯粹用于处理文本。
【解决方案3】:

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

program-1 > program-2

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

std::string  variable;

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

【讨论】:

  • 但这不会限制仅来自 program-1 的输入吗?我认为如果一个人只是想从一个正在运行的程序中获取输出并且仍然能够切换标准输入,那么这种方法可能并不理想。
  • 对。我们没有关于如何运行程序的任何信息,所以这只是一种可能的解决方案(假设是 Windows/Linux 上的控制台程序)。
【解决方案4】:

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

#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;
}

干杯....

【讨论】:

  • 谢谢大家...但我正在WINDOWS中寻找这个解决方案...你能帮忙吗??
【解决方案5】:

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

所以 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]
   }       

}

然后在命令提示符program-1 &gt; program-2

【讨论】:

  • 感谢您的回答。为什么管道不能在这里工作
【解决方案6】:

在 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;
    }
}

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

【讨论】:

    猜你喜欢
    • 2011-08-28
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    相关资源
    最近更新 更多