Skip to content

Instantly share code, notes, and snippets.

@jherr
Created April 9, 2021 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jherr/f625733bb7e133b9d3ea069b52cf563a to your computer and use it in GitHub Desktop.
Save jherr/f625733bb7e133b9d3ea069b52cf563a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
using namespace std;
// Generic print function that takes an output stream.
// Can be used to print to the screen by passing cout.
// Can be used to print to a file by passing a file stream.
void printSomething(ostream &out, string somethingToSay) {
out << "Something cool:" << somethingToSay << endl;
}
int main() {
// Print to the screen
printSomething(cout, "hey there");
// Writes the same thing to a file
ofstream myfile;
myfile.open("example.txt");
printSomething(myfile, "hey there");
myfile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment