Thread: Reading from file redirect

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

    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    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, 01:06 PM