我需要執行多次FORTRAN程序,這需要用戶每次插入4個數字值。 我發現了一個解決方案,使用Python腳本自動完成此操作......此腳本基本上在每次迭代時創建一個包含以下行的.sh文件(a.out是我必須自動執行的FORTRAN程序的名稱)C++系統()導致「文本文件忙」錯誤
./a.out<<EOF
param01
param02
param03
param04
EOF
使其可執行,並執行它。
所以,我試圖做的在C++一樣......我寫的東西像
int main()
{
long double mass[3] = {1.e+10,3.16e+10,1.0e+11};
double tau[3] = {0.5,0.424,0.4};
double nu[3] = {03.0,4.682,10.0};
long double Reff[3] = {1.0e+3,1.481e+3,3.0e+3};
int temp=0;
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;
temp=system("chmod +x shcommand.sh");
temp=system("./shcommand.sh");
}
return 0;
}
但是當我跑我的C++程序,我收到以下錯誤消息
sh: 1: ./shcommand.sh: Text file busy
sh: 1: ./shcommand.sh: Text file busy
sh: 1: ./shcommand.sh: Text file busy
在上一次迭代完成之前,是否與嘗試修改.sh文件的C++程序有關? 我在網上看,我似乎明白了命令完成後,系統()命令onlyreturns ...
爲什麼你從來不使用'system'的結果?你沒有錯誤檢查。 –
在嘗試運行之前是否關閉了該文件? (例如outfile.close()) – swdev