1

I know that the function:

system("myfile.sh")

exec a bash script. Ok but now I want to redirect the output to my program to ensure the reading. For example the script date.sh give me the date of my system, and i want to see it on my program with std::cout << OUTPUTDATE; Is it possible? How?

Tobias
  • 9,170
  • 3
  • 24
  • 30
  • Duplicate of: [Best way to capture stdout from a system() command so it can be passed to another function](http://stackoverflow.com/questions/125828/best-way-to-capture-stdout-from-a-system-command-so-it-can-be-passed-to-another) – Darcara Oct 09 '11 at 10:18

1 Answers1

6

Use popen instead of system.

The function popen will give you a FILE * you can read from.

FILE *script = popen("myfile.sh", "r");
while (fgets(line, LENGTH, script)) {
    /* ... */
}
pclose(script);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • With this method, can I send easly and without error the output to a client on my network? Thx –  Oct 09 '11 at 10:23
  • @user840718 With this method you can capture the output of "myfile.sh". I don't know about the clients on your network :-) – cnicutar Oct 09 '11 at 10:24