Retrieving the results of an operating-system call

Oct 17, 2016 at 12:22pm
If I run an operating-system program, say

1
2
3
4
5
6
7
8
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   system( "dir" );
}


then this runs, but the output is to the screen and not immediately available to the program. I could re-direct the output to a file, then read the file: something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

string readTempFile();

int main()
{
   system( "dir > tempfile" );
   cout << "Contents of file are:\n" + readTempFile();
}

string readTempFile()
{
   string text( "" );
   string line;

   ifstream in( "tempfile" );
   while( getline( in, line ) ) text.append( line + "\n" );
   in.close();

   return text;
}


However, this seems very messy. Is there any way of getting operating-system results straight back to the program without having to go through the filesystem?

"dir", as run from the Windows command line is just an example - not what I was ultimately hoping to use.



Last edited on Oct 17, 2016 at 12:23pm
Oct 17, 2016 at 12:37pm
Don't use system is the first answer.

What you're doing here is asking the operating system to run a completely different application (with no link to your program - the OS simply launches the other application as if you had typed it at the command line, because that's basically what it's doing with system); a whole different program. It could be doing anything, anything at all, and you're hoping that it will write some output to stdout, and that maybe you can intercept the output of that completely different application. This is an inherently awkward thing to do.

Your operating system provides functions for you to use to start other processes. You should use those instead. Here's a windows example to just create the process:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx
and here's one that redirects the output:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

As you can see, it's often awkward. Remember, what you're doing is awkward. You're running a completely different application and you're trying to capture the output from it, live.

Some third party libraries include ways of making it easier for you; QT provides QProcess, for example.
Last edited on Oct 17, 2016 at 12:59pm
Oct 17, 2016 at 1:52pm
Some third party libraries include ways of making it easier for you; QT provides QProcess, for example.

I've also used the Process class provided by Poco, which allows for redirection and retrieval of the standard output/error from the process being run.
Last edited on Oct 17, 2016 at 1:53pm
Oct 18, 2016 at 1:40am
Use the POSIX call popen (_popen in windows)?
Oct 18, 2016 at 9:39am
Many thanks for the suggestions. I definitely need to be less lazy than just using system() all the time. I'll probably start with mbozzi's suggestion first, but they are all helpful (and have given me some learning to do).
Topic archived. No new replies allowed.