Input redirection in C ++

I was told that to redirect from standard input to a file I need to do the following:

static std::ifstream inF("inpur.txt"); std::cin.rdbuf(inF.rdbuf()); 

and every call to std :: cin will be redirected to input.txt. but my question is: do I need to open inF? and if so, where do I need it?

+4
source share
3 answers

That is beauty. You already did this by declaring the object and passing the string to the explicit ifstream constructor.

The file opens in TEXT mode.

Refer this

+2
source

You opened it by calling it with a string constructor.

+4
source

Your code is ok. Make a backup, although from the original cin.rdbuf - you may need to reset in case of an error.

+1
source

All Articles