1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
void display_stars( int rows = 0, int cols = 0, char star_character = '+', std::ostream& stm = std::cout );
int main()
{
display_stars( 7, 10 ); // star_character is '+' (default), output stream is std::cout (default)
display_stars( 5, 17, '*' ); // star_character is '*', output stream is std::cout (default)
const std::string file_name = "Pattern.txt" ;
{
std::ofstream file(file_name); // open the file for input
// output stream is file
display_stars( 7, 10, '+', file ); // star_character is '+', output stream is file
display_stars( 5, 17, '*', file ); // star_character is '*', output stream is file
} // file is automagically closed when its life-time is over
// dump the contents of the file to see what was written into it
std::cout << "\n-----------------\ncontents of file " << std::quoted(file_name) << ":\n------------------\n" ;
std::cout << std::ifstream(file_name).rdbuf() ;
}
void display_stars( int rows, int cols, char star_character, std::ostream& stm )
{
for( int down = 0; down < rows; ++down )
{
for( int across = 0; across < cols; ++across ) stm << star_character ;
stm << '\n' ;
}
stm << '\n' ;
}
|