简体   繁体   中英

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

I have 2 C programs. Say one is 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.

How can I do this?

You can use the popen function for this:

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

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

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);

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....

On windows u can use this example...

#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..:)

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

So program-2.c should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM