Thread: Input: File or stdin?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    21

    Input: File or stdin?

    Hey folks,

    I've written a program in C++ that takes an input filename as a command line parameter, and that filename gets passed to the processing function as a char*, then the processing function declares an ifstream and does it's work.

    What I would like to do, is if no filename is given at the command line as an argument, then stdin is used for input. The problem is, what can I pass to the processing function as a parameter to tell it to use stdin? I assume I'd have to change the parameter type, but to what? I doubt I need to make another version of that function...

    Anyone wanna give me a hint? Does this rambling even make sense?

    Thanks,
    ~arker
    Last edited by Arker; 02-11-2003 at 07:55 AM.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    i don't know what stdin is... so... anyways.. why not just create two functions with the same name and use an IF statement and depending on the IF statement one of the functions is called... or pass both the filename & stdin to the same function and do an IF statement there... or lastly... (i think this last one would work and be the best) use a TEMPLATE so the program decides what type to pass into the function at runtime instead of declaring it in the code before hand..

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Derive an instance of an istream that either points to an ifstream or cin depending on whether a command line argument has been passed. Something like -

    istream* is;
    if (cl)
    is = new ifstream(cl);
    else
    is = &cin;

    Don't forget to delete if it's a fstream.
    Joe

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    Thank you JoeSixpack. With a little messing around with the stuff I had already coded, that worked perfectly.

    ~arker

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM