QUESTION: Dear Henry,

How are you? I was wondering if I could bother you with the following question:

There is a compiled command line program (available for both Windows and Linux: http://www.fourmilab.ch/random/) that I execute like this:

"ent.exe -b -t input.txt"

Here the file "input.txt" is simply a text file containing 1's and 0's, one value per line.

Now I'd like to run this program from within my C++ code. I know there is "system" command that could be used like this:
system("ent.exe -b -t input.txt"). However, the tricky part is reading the ent program's output so that my own program could continue using it. I'd like to read it into a string that I could then parse. This is the part that I don't know. I'd be very thankful if you could help me with this a little bit.

If you have time, maybe you could also comment on how the same works on Linux. What replaces the "system" command in Linux?


ANSWER: Hello,

First I am sorry, I am answering very late because of my holiday.
In C/C++ you could run systems commands but it is a bit complex. I once did those back in School but I can't fully figure it out but i do remember it has to do with writing at the kernel level. Kindly try this:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main( int argc, char **argv ) {
   pid_t child = 0;
   child = fork();
   if (child < 0) {
       fprintf( stderr, "process failed to fork\n" );
       return 1;
   if (child == 0) {
   else {
       execl( "ent.exe -b -t input.txt")
   return 0;

my advise is that you study IPC c/c++ programming to better do this. I can't help much as I haven't worked in that field in a long long time :(

---------- FOLLOW-UP ----------

QUESTION: Dear Henry,

Thank you for your help. This is, indeed, a good way. However, I'm getting an error message when compiling. The wait() function does not have enough parameters.

I'm getting this line to do what I want very nicely:

system("./ent -b -t input.txt");

However, it writes the results on a screen.
1. What is the best way to write it into a text array (char[])?
2. With the above line, can I be certain that the rest of the program will wait around until the command executes? If not is there a simple way to achieve that?

Thank you so much in advance.


Hello Andres,

Like I said earlier, try to study more on IPC and specifically on process fork.
Now, if you want it to write into a text array, you have to figure out how to do that with linux or system command since the first command you had was "ent.exe -b -t input.txt" and the problem was how to embed that in a C programme which I showed you using the excl() command. there are other excl functions just google them out. Also, the wait function in the if(child==0) is saying once you are in a child process wait. else if in the parent process, call the excl function. The fork command is a way a process duplicates itself. Again, I advise you to study more about this if you really need to use. I have only worked with this over 4 years ago. so, I can't really help alot right now and since it involves timing. I might have to study it again and write afew programme then I get back to you when am free :)


All Answers

Answers by Expert:

Ask Experts




I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.


I have a 3- year- work experience in software Engineering.

I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.