Reading from file redirect

This is a discussion on Reading from file redirect within the C Programming forums, part of the General Programming Boards category; Hi, this is a very basic question, but i haven't been able to find anything useful on the net so ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    Reading from file redirect

    Hi,

    this is a very basic question, but i haven't been able to find anything useful on the net so far. so i have my program that reads from file in two ways:

    ./prog -i input

    or

    ./prog < input

    i am running this under unix

    and the way i am readin it is like this:

    Code:
    int fd;
    
    if(args->i != NULL){
        fd = open(args->i,0);
      }else{
        fd = 0;
      }
      if(fd < 0){
        printf("ERROR could not open input file %s\n",args->i);
        return -1;
      }
    
    while((c = read(fd,buf,BUFSIZ)) > 0){
        ...
    }
    so i can check if my -i is defined or not , but how do i check that for my redirect?

    thank you

    baxy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,769
    Quote Originally Posted by baxy
    so i can check if my -i is defined or not , but how do i check that for my redirect?
    With the input redirection, you get input from stdin, so presumably you would use say, file descriptor 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    yes , but how do i check if there is anything that is redirected at all ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    On the rare occasions where it might matter.
    ISATTY(3) Linux Programmer's Manual ISATTY(3)

    NAME
    isatty - test whether a file descriptor refers to a terminal

    SYNOPSIS
    #include <unistd.h>

    int isatty(int fd);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,769
    Logically, if the user did not specify the -i command line argument, then the input should come from stdin. Maybe it was redirected, maybe the user really wants to enter it on the spot, whatever.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    On the rare occasions where it might matter.
    ISATTY(3) Linux Programmer's Manual ISATTY(3)

    NAME
    isatty - test whether a file descriptor refers to a terminal

    SYNOPSIS
    #include <unistd.h>

    int isatty(int fd);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    If you want to check whether standard input is a file or something else, you can use fstat(STDIN_FILENO, &info); to get the statistics on the descriptor, then S_ISREG(info.st_mode) to see if it is a regular file or not. For example:
    Code:
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>
    
    #define  FD_TYPE_FILE 0
    #define  FD_TYPE_DIR 1
    #define  FD_TYPE_CHAR 2
    #define  FD_TYPE_BLOCK 3
    #define  FD_TYPE_FIFO 4
    #define  FD_TYPE_SYMLINK 5
    #define  FD_TYPE_SOCKET 6
    #define  FD_TYPE_UNKNOWN 7
    
    /* Return FD_TYPE_, or -1 if error.
    */
    int fd_type(const int descriptor)
    {
        struct stat  info;
        int   result;
    
        if (descriptor == -1) {
            errno = EINVAL;
            return -1;
        }
    
        result = fstat(descriptor, &info);
        if (result == -1)
            return -1;
    
        if (S_ISREG(info.st_mode))
            return FD_TYPE_FILE;
        else
        if (S_ISDIR(info.st_mode))
            return FD_TYPE_DIR;
        else
        if (S_ISCHR(info.st_mode))
            return FD_TYPE_CHAR;
        else
        if (S_ISBLK(info.st_mode))
            return FD_TYPE_BLOCK;
        else
        if (S_ISFIFO(info.st_mode))
            return FD_TYPE_FIFO;
        else
    #ifdef S_ISLNK
        if (S_ISLNK(info.st_mode))
            return FD_TYPE_SYMLINK;
        else
    #endif
    #ifdef S_ISSOCK
        if (S_ISSOCK(info.st_mode))
            return FD_TYPE_SOCKET;
        else
    #endif
            return FD_TYPE_UNKNOWN;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect make output to some other file..
    By jgtech in forum Linux Programming
    Replies: 1
    Last Post: 08-07-2011, 11:35 PM
  2. Intercept and redirect file requests
    By GamezR2EZ in forum Windows Programming
    Replies: 10
    Last Post: 06-04-2010, 01:10 PM
  3. file output redirect from child
    By rvervecken in forum C Programming
    Replies: 4
    Last Post: 08-12-2009, 10:37 AM
  4. Who to redirect stdout to a file in C
    By edugarcia in forum Linux Programming
    Replies: 3
    Last Post: 10-01-2004, 12:18 AM
  5. redirect stdout to a file?
    By Brian in forum C Programming
    Replies: 5
    Last Post: 01-15-2002, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21