开发者

redirecting input in c++

开发者 https://www.devze.com 2023-01-15 04:42 出处:网络
i was told that to redirect from standard input to file i need to do the following: static std::ifstream inF(\"inpur.txt\");

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

static std::ifstream inF("inpur.txt");
std:开发者_JS百科: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 i do, where do i need to do this?


You opened it by calling it with the string constructor.


That's the beauty. You already did so while declaring the object and passing the string to the explicit constructor of ifstream.

The file is opened in TEXT mode.

Refer this


Your code as-is fine. Do make a backup though of the original cin.rdbuf -- you may need to reset it in case of an error.

0

精彩评论

暂无评论...
验证码 换一张
取 消