Command line arguments and file redirection

This is a discussion on Command line arguments and file redirection within the C++ Programming forums, part of the General Programming Boards category; Does any one know about any example of file redirection by using command line arguments such as: int main (int ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    Command line arguments and file redirection

    Does any one know about any example of file redirection by using command line arguments such as:

    int main (int argc, char *argv[])
    {

    ..........

    }

    I wrote a program that reads from a file by using
    the above structure but when I am running I run this way:

    <progname> file.txt

    file.txt is a file to be read from as an input to the program and my program outputs to the screen.

    but what I have been asked in an assignment is like this when running on the console:

    <program name> file1.txt > file2.txt.

    if anyone has any example somewhere or how to implement please your help is needed

    thanks in advance.

    By abdi

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What behavior is the program supposed to have when this occurs?



    "but what I have been asked in an assignment is like this when running on the console:

    <program name> file1.txt > file2.txt. "
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    >>What behavior is the program supposed to have when this occurs?

    methinks, to read from file1 and write into file2...


    if thats what you want then you should try the following steps

    1. check if argc == 3
    2. check if argv[2] == '>'
    3. check if argv[1] ie file1.txt exists or not
    4. if exists read from it and write using fstream.h

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can handle this rather easily if you treat cout as a generic std::ofstream&.

    IE

    Code:
    #include <iostream>
    #include <fstream>
    
    int main(int argc, char* argv[]) {
      std::ostream& outStream = std::cout;
      std::istream in;
      std::ofstream outFile;
       // handle args
       if (/* output redirection needed */) {
          outFile.open(/* appropriate name */);
          outStream = outFile;
       }
       // else outStream will still be cout
       // output stuff through outStream
    }
    This way, you don't have to write two different methods for when you are using different output streams.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Command Line arguments question
    By micpi in forum C Programming
    Replies: 7
    Last Post: 04-24-2007, 08:46 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM

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