C++: assign cin to an ifstream variable?

前端 未结 2 768
再見小時候
再見小時候 2020-12-20 12:39

You know the common stdio idiom that stdin is specified by a filename of \"-\", e.g.

if ((strcmp(fname, \"-\"))
    fp = fopen(fname);
else
             


        
2条回答
  •  余生分开走
    2020-12-20 12:48

    • Save the initial streambuf of cin into a variable
    • Change the streambuf of cin to the one from the file.
    • Do what you need to do
    • And don't forget to restore cin streambuf before closing your file -- RAII may help.

提交回复
热议问题