Thread: standard input / output check

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    standard input / output check

    I'm trying to check the command line arguments for a -i input_filename flag. If it's present I want to read the input stream from the file named there. If it's not I want to read the input from standard input.

    Also, I want to check the command line arguments for a -o output_filename, if it's present it'll write the output stream named there, if not it'll write the input to standard output.

    My question is whats the best way to go about doing these checks for my program? Thanks, let me know.

    So far this is what I have:

    FILE *fp;

    if (!strcmp(argv[1], "-i") && !strcmp(argv[3], "-o")) { // Ex: -i input.txt -o output.txt

    /* Input File */ fp = fopen(argv[2],"r");
    // Output File output = fopen(argv4], "w");
    }
    Last edited by benjammin5; 10-25-2011 at 11:21 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      FILE *fp;
      if ( argc == 2 ) {
        printf("Input from file\n");
        fp = fopen(argv[1],"r");
      } else {
        printf("Input from stdin\n");
        fp = stdin;
      }
      int ch;
      while ( (ch=fgetc(fp)) != EOF ) fputc(ch,stdout);
      fclose(fp);
      return 0;
    }
    
    
    $ ./a.out 
    Input from stdin
    hello
    hello
    $ ./a.out < input.txt 
    Input from stdin
    123
    $ ./a.out input.txt 
    Input from file
    123
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    how do I use fopen on the file name if they use standard input < filename.txt? my program uses f open if they put in the -i filename.txt

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    < redirections are handled by the shell, not your program.
    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
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    For a.out < filename.txt or cat filename.txt | a.out
    no filename is needed in your program, use stdin like:

    Code:
    char line[BUFSIZ];
    while( fgets(line,BUFSIZ,stdin) )
    {
      ...
    }
    Last edited by BillyTKid; 10-26-2011 at 01:16 AM.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by benjammin5 View Post
    how do I use fopen on the file name if they use standard input < filename.txt? my program uses f open if they put in the -i filename.txt
    You don't. It's their choice to use redirection instead of `-i` option, and your program shouldn't care.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-11-2011, 10:40 AM
  2. Replies: 37
    Last Post: 02-27-2011, 09:46 AM
  3. Where does the standard output go the following program
    By Overworked_PhD in forum Linux Programming
    Replies: 1
    Last Post: 02-23-2009, 08:48 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. filestreams (fstream) and standard input/output (stdlib)
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:14 AM