#include <iostream>
#include <fstream>
// accept an output stream (which may be a file stream) to the function
std::ostream& ShowProgramHeader( std::ostream& stm )
{
// use the stream which was passed in the function
returnstm << "the program header here (whatever it is)\n" ;
}
// accept an output stream (which may be a file stream) to the function
// use the stream which was passed in the function
int SquareMachine( std::ostream& stm ) ;
int main()
{
constchar file_name[] = "sTable.out" ;
std::ofstream fout(file_name) ; // out|trunc is the default
if( fout.is_open() ) // not fout.is.open()
{
ShowProgramHeader(fout) ; // pass the stream to the function
// ...
constint result = SquareMachine(fout) ; // pass the stream to the function
// rest of the program
}
else
{
std::cerr << "failed to open output file " << file_name << '\n' ;
return 1 ;
}
}