0

For example, Guess I have a compiled c program, named as 'binaryOutput'. And in Unix environment, [root@blablabla ~ ] ./binaryOutput print out some result like this [0] [1] [0] [1] [1]

I want to using these result as the input of another c file.

In C lanugage, I can run the file.

system("./binaryOutput") ;

After the code, I want to add the numbers as an array's input. How can I do it?

JongHyeon Yeo
  • 879
  • 1
  • 10
  • 18
  • You should look into `popen()` from here: https://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/popen.html –  Mar 27 '18 at 05:26
  • 3
    Possible duplicate of [How can I run an external program from C and parse its output?](https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output) – Lanting Mar 27 '18 at 05:37
  • Would (from a shell): `./binaryOutput|myprog` be acceptable? In `myprog` you just read `stdin`. It is the simplest way to do this. – cdarke Mar 27 '18 at 06:33

1 Answers1

2

popen example. You can get the output of the command.

#include <stdio.h>

int main(void)
{
  FILE *fp=NULL;
  char line[1024];
  if ( (fp=popen("ls", "r"))==NULL )
  {
    return -1;
  }
  while( fgets(line, 1023, fp)!=NULL )
  {
    printf("read from popen:%s", line);
  }
  pclose(fp);
  return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Junhee Shin
  • 748
  • 6
  • 8
  • 2
    Could you also describe how your code works? It looks like your program collects dir listing. That's not what OP is looking for. The question asks about executing any executables. Not a specific unix command. So tell us the generic approach. –  Mar 27 '18 at 05:29
  • 1
    replace "ls" to what you want. "./binaryOutput" – Junhee Shin Mar 27 '18 at 05:38
  • @MohammadRakibAmin `ls` _is_ part of the set "any executables". – Jabberwocky Mar 27 '18 at 07:47
  • 1
    `fgets(line, sizeof line, fp)` -- there is no reason to reduce the `size` parameter to `fgets` by `1` as you do for `scanf` *field-width* modifier. – David C. Rankin Mar 27 '18 at 07:48
  • @Michael Walz I was unaware you have to figure out what 'set' does an input belongs to, then check if that 'set' covers your desired input. Would it not be more helpful to just mention `your command goes here`? I'm only pointing out ways to be more clear to OP's query. –  Mar 27 '18 at 08:15