Skip to content

Instantly share code, notes, and snippets.

@blippy
Last active March 13, 2018 16:08
Show Gist options
  • Save blippy/e353cb68336a853b951f0f45fd7de251 to your computer and use it in GitHub Desktop.
Save blippy/e353cb68336a853b951f0f45fd7de251 to your computer and use it in GitHub Desktop.
Reading from cin or file
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <string>
void reading()
{
std::string filename = ... ;
std::ifstream ifs;
if(filename.size()>0) ifs.open(filename);
std::istream &is = ifs.is_open() ? ifs : std::cin;
// read inputs
string line;
while(getline(is, line)) {
...
}
if(ifs.is_open()) ifs.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment