开发者

Duplicate std::cin in C++

开发者 https://www.devze.com 2023-02-18 22:50 出处:网络
Is C++ cin a file descriptor and is possible to duplicate using dup()? I want to duplicate the standard input in order to use one for filestream using redirection from the shell and 开发者_Python百科o

Is C++ cin a file descriptor and is possible to duplicate using dup()? I want to duplicate the standard input in order to use one for filestream using redirection from the shell and 开发者_Python百科one is for regular input after getting all the input from the file.


std::cin is bound to standard input file descriptor, in POSIX world it is defined as STDIN_FILENO and its value is usually 0. std::cin is just a special std::istream to read from that descriptor. You can use dup to duplicate it - no problem, you don't have to use std::cin for this purpose at all.


No.

std::cin is an input stream. This is a C++ concept that may under the scenes be implemented using (in part) a file handle. However, all you have access to is the stream object and its members.


You don't need to duplicate the stream, just create an fstream and read the data need.

0

精彩评论

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