2

I am a beginner and trying to write a program for physics computations.

Currently the output being generated by a child program is sent to a separate text file and the parent program opens this file later and reads a value from it - the output is unnecessary to store, only the value is significant. It would be more elegant to use buffer for this purpuse, so that no extra text file would be generated in the process.

My question is how to get the output sent to buffer instead of file (micromegas.out below) and later search for the value in the buffer in the same manner as it is done for the output file in the code below?

string micromegas = "./micromegas_3.2/MSSM/main " + p[0] + " " + p[1] + " " + p[2] + " " + p[3] + " " + p[4] + " > micromegas.out";
// Execute child program and send output to micromegas.out
system(micromegas.c_str());

FILE *fout = fopen("micromegas.out", "r"); // open the output file and search for the value (Omega)
char * buffer =(char *)malloc(512);
long double Xf, calc_omega_hsq;
while(fgets(buffer, 512, fout)) 
{
    if (sscanf(buffer, "Xf=%Lf Omega=%Lf", &Xf, &calc_omega_hsq)) {}
}
fclose (fout);

Output currently stored in file "micromegas.out" "==== Calculation of relic density ===== Xf=2.22e+01 Omega=1.34e+00"

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Gleb
  • 53
  • 1
  • 6

3 Answers3

4

So, I think what you are looking for popen (or _popen in Windows), which will allow you to read the standard out from another process.

You'd do something like FILE *fout = popen(micromegas.c_str(), "r"); instead of the system and fopen lines.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

system doesn't let you access the error or output of the child process. If you want the simplest solution, you're already using it.

If you want to make things more complex, you need to do a few things:

  1. Use fork and exec instead of system. System is a shorthand, easier version of this. fork will make a complete copy of your memory space, while exec will overwrite your current memory with the program you're execing.
  2. Once you're doing this, you can make a pipe. Pipes are means of communicating between a parent process and a child.

There are plenty of examples of how to do this out there, including Redirecting exec output to a buffer or file

Community
  • 1
  • 1
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Using `fork` and `exec` makes for also using the basic `read` to read the output data. Much easier to use `popen`, which does all the heavy lifting for you. – Mats Petersson Jul 30 '13 at 14:45
  • @MatsPetersson Agreed, which is why I upvoted yours immediately when I saw it. But mine provides an alternative as well as a link full of other alternatives (including popen), so I didn't delete mine. – Scott Mermelstein Jul 30 '13 at 14:53
0

You can define a vector with a type double : if you just need to save one value at a time you dont have to declare the vector any bigger than the size=1 .Then you can save your value to the vector if you want to store more values use push_back().

vector<long> Vec (size);
Vec.push_back(the result of calculation);

To access any value from the vector you can use the following :

temporaryNumber=Vec[i];
fer y
  • 505
  • 4
  • 19