Thread: User entering I/O File names at command line

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question User entering I/O File names at command line

    I've finished coding my whole project except for this last part (thank goodness). In all the projects i've done in the past, the input and output file names are set by the programmer in the code itself. In this one the teacher writes "The user will be required to enter the input and output file names at the command line in order to run the program. Appropriate error messages should be displayed in the event that user failes do do so." The input and output files are both text files (don't think that matters but I want to give all the info needed for anyone who might be able to help me). Anyone have any clue on how to code this?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This would be an appropriate time to make use of the arguments passed to main:
    int main ( int arc, char **argv )

    The way your program should be run from the command line is something like this:
    C:\> programname in.txt out.txt

    Inside your program, the arguments you pass to main will have that information. argc is the number of items, in this case 3. argv is an array of strings that contains each item, so argv[0] is the program name, argv[1] is the name of the input file, and argv[2] is the name of the output file.

    Provided the arguments were entered properly, which they should since all users are highly intelligent people who follow directions, you can open the files like so:
    ifstream in ( argv[1] );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Talking

    The internet still amazes me sometimes with all that it can do. Posting a topic and getting a free response this quick is great.

    I'm assuming you meant:

    int main(int argc, char **argv)

    instead of int arc.

    I don't have to include argc if i'm not using it do I? I'm thinking I could test if the input and output file names were entered by just doing something like:

    Code:
    if(!in)
    {
       cout << "Not a valid input file.";
    }
    The two ** before argv are needed and not just space holders for something i'm not seeing, correct? I'm assuming they have something to do with it being an array of strings and that they're required, but I just want to make sure. Are there any files I need to include to use this code passed to main?

    Thanks ALOT for your quick response and help. I've been doing this pencil and paper so far, hopefully when I type this all up it works out well.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >instead of int arc.
    Yes, that was a typo. But it really doesn't matter what you call them, argc and argv are just the most common names.

    >I don't have to include argc if i'm not using it do I?
    Yes, you do. If you include one then you have to include the other as well, even if you don't use it.

    >The two ** before argv are needed and not just space holders
    >for something i'm not seeing, correct?
    ** means a pointer to a pointer, it's a way of saying an array of pointers. Another way to write argv is
    char *argv[]

    >Are there any files I need to include to use this code passed to main?
    Nope.

    >I've been doing this pencil and paper so far
    I do that too, write and debug the program on paper and then when I sit down to the computer the program is virtually bug free. (hopefully, sometimes I miss something).

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    Awesome, thanks alot. Hopefully you don't hear from me again.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    I'm using Unix at the school (alpha). To compile I type cxx *.cpp and it gives me an a.out file. If I need to type the input and output names at the command line, how do I do that? When I compile it gives me this msg:

    Code:
    ld (prelink):
    processInfile.o compressed: processInfile(ifstream&, char&): multily defined
    ld:
    processInfile.ocompressed: processInfile(ifstream&, char&): multiply defined

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    Anyone?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM