How to check if the program is run from a console?

后端 未结 5 1080
粉色の甜心
粉色の甜心 2020-12-01 00:50

I\'m writing an application which dumps some diagnostics to the standard output.

I\'d like to have the application work this way:

  • If it is run from a s
5条回答
  •  暖寄归人
    2020-12-01 01:36

    After trying quite many different api's and calls, I've found out only one working approach:

    bool isConsole = isatty(fileno(stdin));
    

    Stdout cannot be used as you can run console application and redirect output to file using >log.txt switch. Suspect it's also possible to redirect input as well, but it's quite rarely used functionality when running console applications.

提交回复
热议问题