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

10,572

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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

Share:
10,572
Ronin
Author by

Ronin

Updated on June 21, 2022

Comments

  • Ronin
    Ronin almost 2 years

    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?

  • stijn
    stijn over 12 years
    +1 as IPC is almost always the better and scalable solution, unless the two programs are purely for procesing text.
  • Shamim Hafiz - MSFT
    Shamim Hafiz - MSFT over 12 years
    But won't that restrict input from program-1 only? I think if one was simply interested to fetch the output from one running program and be still able to switch around standard input, this method may not be ideal.
  • Bo Persson
    Bo Persson over 12 years
    Right. We don't have any info on how the programs are to be run, so this is just one possible solution (assuming console programs on Windows/Linux).
  • Ronin
    Ronin over 12 years
    thank u all ... but I am looking for this solution in WINDOWS ... Can u plz help ??
  • Ronin
    Ronin over 12 years
    thank u all ... but I am looking for this solution in WINDOWS ... Can u plz help ??
  • Mat
    Mat over 12 years
  • EHM
    EHM over 3 years
    Thank you for the answer. why wouldnt piping work here