C++ C++;系统()原因;“文本文件忙”;错误

C++ C++;系统()原因;“文本文件忙”;错误,c++,bash,C++,Bash,我需要多次执行FORTRAN程序,该程序要求用户每次插入4个数值。 我找到了一个用Python脚本自动实现的解决方案……该脚本基本上在每次迭代时创建一个包含以下行的.sh文件(a.out是我必须自动执行的FORTRAN程序的名称) 与C++程序在前一次迭代结束之前修改.h文件有什么关系吗? 我在网上查看了一下,似乎理解了system()命令仅在命令完成后才返回…这似乎是因为shell无法读取脚本,因为您的程序仍在打开它 尝试添加outfile.close()

我需要多次执行FORTRAN程序,该程序要求用户每次插入4个数值。 我找到了一个用Python脚本自动实现的解决方案……该脚本基本上在每次迭代时创建一个包含以下行的.sh文件(a.out是我必须自动执行的FORTRAN程序的名称)

与C++程序在前一次迭代结束之前修改.h文件有什么关系吗?


我在网上查看了一下,似乎理解了system()命令仅在命令完成后才返回…

这似乎是因为shell无法读取脚本,因为您的程序仍在打开它


尝试添加
outfile.close()system()

之前,似乎是因为shell无法读取脚本,因为您的程序仍在打开脚本


尝试添加
outfile.close()
在调用
system()

之前,您试图运行一个打开的文件,这不是一个好主意。在
chmod
ding/运行它之前关闭它:

for (int i=0; i<3; i++)
{
    {
        ofstream outfile("shcommand.sh");
        outfile << "./a.out<<EOF" << endl << mass[i] << endl << nu[i] << endl << Reff[i] << endl << tau[i] << endl << "EOF" << endl;
        // the file is closed when outfile goes out of scope
    }
    temp=system("chmod +x shcommand.sh");
    temp=system("./shcommand.sh");
}

您正在尝试运行一个打开的文件,这不是一个好主意。在
chmod
ding/运行它之前关闭它:

for (int i=0; i<3; i++)
{
    {
        ofstream outfile("shcommand.sh");
        outfile << "./a.out<<EOF" << endl << mass[i] << endl << nu[i] << endl << Reff[i] << endl << tau[i] << endl << "EOF" << endl;
        // the file is closed when outfile goes out of scope
    }
    temp=system("chmod +x shcommand.sh");
    temp=system("./shcommand.sh");
}

为什么从不使用
系统的结果
?检查时没有错误。在尝试运行该文件之前是否关闭了该文件?(例如outfile.close())为什么从不使用
系统的结果?检查时没有错误。在尝试运行该文件之前是否关闭了该文件?(例如outfile.close())
sh: 1: ./shcommand.sh: Text file busy
sh: 1: ./shcommand.sh: Text file busy
sh: 1: ./shcommand.sh: Text file busy
for (int i=0; i<3; i++)
{
    {
        ofstream outfile("shcommand.sh");
        outfile << "./a.out<<EOF" << endl << mass[i] << endl << nu[i] << endl << Reff[i] << endl << tau[i] << endl << "EOF" << endl;
        // the file is closed when outfile goes out of scope
    }
    temp=system("chmod +x shcommand.sh");
    temp=system("./shcommand.sh");
}
for (int i=0; i<3; ++i) {
    FILE *fd = popen("./a.out", "w");
    assert(fd!=NULL); // do proper error handling...
    fprintf(fd, "%Lf\n%f\n%Lf\n%f\n", mass[i], nu[i], Reff[i], tau[i]);
    fclose(fd);
}