6

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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Ronin
  • 2,027
  • 8
  • 32
  • 39
  • 1
    Do you actually mean to pass data between two separate executables (program-1.exe and program-2.exe) or two classes or functions in separate code files, which is what you've shown so far? – ChrisBD Jan 04 '12 at 07:57
  • i need both - Windows n linux... separate solutions. can u help ? – Ronin Jan 04 '12 at 09:43
  • @ChrisBD :: actually I need to know the both situations.... – Ronin Jan 04 '12 at 09:52

6 Answers6

11

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);
Mat
  • 202,337
  • 40
  • 393
  • 406
  • thank u all ... but I am looking for this solution in WINDOWS ... Can u plz help ?? – Ronin Jan 04 '12 at 09:45
  • http://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api – Mat Jan 04 '12 at 09:58
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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1 as IPC is almost always the better and scalable solution, unless the two programs are purely for procesing text. – stijn Jan 04 '12 at 07:59
2

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);
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 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. – Shamim Hafiz - MSFT Jan 04 '12 at 08:01
  • 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). – Bo Persson Jan 04 '12 at 08:46
2

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

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
1

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

user1129769
  • 46
  • 1
  • 2
0

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

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44