I am completely new to the c++ and I have problem with managing input. I have a program where I want to be possible to read input from console but also from the file.
I have class with private field std::istream &input_stream;
and in constructor I set this input_stream
as std::cin
, however when my program is run with flag -i-FILENAME
I want input_stream
to be that file.
class FlagsParser
{
private:
std::istream &input_stream;
public:
FlagsParser() : input_stream(std::cin){}
void ParseFlags(int count,char *flags[]) -> arguments I get from main
{
for(int i =1;i<count;++i){
std::string flag = flags[i];
if(flag.rfind("-i",0) == 0){ // check if the arg starts with -i
std::ifstream readFile(flag.substr(2));
-> Here i want to set input_stream to readFile and I have no idea how.
...
What should I do ? Thank you for any help. (Maybe I am doing it completely wrong ?)