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);
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With