So from the title. I'm still pretty wonky on the limitations of file streams. However is the following possible, given

ofstream myfile;
myfile.open ("example.txt");
while(int i=0; i<1000;i++){
  functionfoo(i);
}

myfile.close();

and inside functionfoo we have

myfile << object_1;
//some other computations, essentially a placeholder

or should I open and close the filestream inside functionfoo and pass the file name?

It is better to pass the ofstream as a parameter instead of making it a global because globals are generally frowned upon. Pass by reference, you can't pass it by value.