C - Is the input a tty?

15. March 2012 08:00

 

In a C program it can be useful sometimes to know if the stdin / stdout / stderr is actually connected to a termianl or not. There is a function alled isatty() which is dedicated to detecting this. An example is below.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
    if (argc < 2) {
        printf("ERROR: Need a file descriptor\n");
        exit(0);
    }
    printf("%d\n", isatty(atoi(argv[1])));
    return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading