// This program demonstrates how to write to an output file from a different function // than the one the file is declared and opened in. #include #include using namespace std; void testfunct(ofstream & fout, int i); // notice the & between ofstream and fout - necessary void testfunct2(ofstream & fout); int main() { int i; ofstream fout; fout.open ("output.txt"); cout << " Please enter an integer " << endl; cin >> i; testfunct(fout, i); testfunct2(fout); fout.close(); return 0; } void testfunct(ofstream & fout, int i) // again, notice the & { fout << "i is " << i << endl; } void testfunct2(ofstream & fout) { fout << " Hello, how are you today? " << endl; }