is there a way to check if external data is piped to a program or if program runs on its own?

若如初见. 提交于 2020-01-04 05:37:24

问题


That is the data is already provided by another program, or user must type it manually. I mean these two states:

dir /b /s *.* | myprogram

and

myprogram

In second case the program would be waiting for user input. Is there a way to prevent that ?


回答1:


POSIX has isatty(), whereas Visual C++ has _isatty(), both of which will return true if a file descriptor is a terminal or command line, e.g. isatty(fileno(stdin)).




回答2:


Yes. Use GetStdHandle to get the STD_INPUT_HANDLE and then check that the handle is of the type FILE_TYPE_PIPE in the first case or FILE_TYPE_CHAR in the second case by calling GetFileType on it.



来源:https://stackoverflow.com/questions/10574797/is-there-a-way-to-check-if-external-data-is-piped-to-a-program-or-if-program-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!