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

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


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.