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:
but if redirect file, write file:code:hello world
i know can done, can't find out how. appreciated.code:hello world
looks ubuntu forum lucky place, got answer after posting this. can checked isatty() function:
here how runs:code:#include <unistd.h> #include <stdio.h> int main() { if(isatty(fileno(stdout))) printf("yes\n"); else printf("no\n"); return 0; }
visit http://linux.die.net/man/3/isatty know isatty() or write in terminal: man 3 isattycode: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$
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
Post a Comment