bash - C++ system() causes "Text file busy" error -


i need execute multiple times fortran program, requires user insert 4 numeric values each time. found solution make automatically python script...this script creates @ each iteration .sh file containing following lines (a.out name of fortran program have execute automatically)

./a.out<<eof param01 param02 param03 param04 eof 

makes executable, , executes it.

so, i'm trying same in c++...i wrote like

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;    (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;     } 

but when run c++ program, following error message

sh: 1: ./shcommand.sh: text file busy sh: 1: ./shcommand.sh: text file busy sh: 1: ./shcommand.sh: text file busy 

has c++ program trying modify .sh file before previous iteration finished? looked online, , seemed understand system() command onlyreturns after command has been completed...

you trying run open file, isn't such idea. close before chmodding/running it:

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;         // file closed when outfile goes out of scope     }     temp=system("chmod +x shcommand.sh");     temp=system("./shcommand.sh"); } 

incidentally, shell mess can avoided writing straight standard input of program (e.g. popen):

for (int i=0; i<3; ++i) {     file *fd = popen("./a.out", "w");     assert(fd!=null); // proper error handling...     fprintf(fd, "%lf\n%f\n%lf\n%f\n", mass[i], nu[i], reff[i], tau[i]);     fclose(fd); } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -