Last active
March 13, 2018 16:08
-
-
Save blippy/e353cb68336a853b951f0f45fd7de251 to your computer and use it in GitHub Desktop.
Reading from cin or file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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