【发布时间】:2013-07-30 18:15:28
【问题描述】:
我是一个初学者,正在尝试编写一个物理计算程序。
当前子程序生成的输出被发送到一个单独的文本文件,父程序稍后打开这个文件并从中读取一个值——输出不需要存储,只有值是有意义的。为此目的使用缓冲区会更优雅,因此在此过程中不会生成额外的文本文件。
我的问题是如何将输出发送到缓冲区而不是文件(下面的 micromegas.out),然后以与下面代码中的输出文件相同的方式搜索缓冲区中的值?
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);
当前存储在文件“micromegas.out”中的输出“==== 计算遗物密度 ===== Xf=2.22e+01 Omega=1.34e+00”
【问题讨论】:
标签: c++ file io-redirection