Skip to main content

Thread: Checking output stream in C


i trying write c program determine whether program's output tied terminal or redirected other file descriptor.

thought of comparing fileno(stdout) stdout_fileno, not work in case of redirection, of redirections handled dup2.

more clear, want is, say, if run program in terminal, print:
code:
hello world
but if redirect file, write file:
code:
hello world
i know can done, can't find out how. appreciated.

looks ubuntu forum lucky place, got answer after posting this. can checked isatty() function:
code:
#include <unistd.h> #include <stdio.h>  int main() {         if(isatty(fileno(stdout))) printf("yes\n");         else printf("no\n");         return 0; }
here how runs:
code:
zobayer@zobayer:~/lab/system/ls$ g++ test.cpp zobayer@zobayer:~/lab/system/ls$ ./a.out yes zobayer@zobayer:~/lab/system/ls$ ./a.out > out.txt zobayer@zobayer:~/lab/system/ls$ cat out.txt no zobayer@zobayer:~/lab/system/ls$
visit http://linux.die.net/man/3/isatty know isatty() or write in terminal: man 3 isatty

hope helpful if looks in future.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] Checking output stream in C


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support