CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2010
    Posts
    6

    command line arguments function

    I'm having an issue with writing the body statements for a function called create_names which purpose is to create the names for all of the files (my guess the output files).

    Here is the function "create_names"

    Code:
    void create_names(int argc, char* argv[], string& iFileName,
                                  string& oFileNameAll, string& oFileNameGood, 
                                  string& oFileNameSmall, string& oFileNameRepeat)
    When I execute a program as ./MyProgram.exe xyz, xyz is the prefix for my files.

    If the input name is xyz, the input will be xyz.txt. I'm suppose to build the output names as xyz_output_all.txt, xyz_output_good.txt, xyz_output_small.txt and xyz_output_repeat.txt.
    ------------------------------------------------------

    Can someone just explain to me how am I suppose to use the string& to create the files? Am i suppose to use an ofstream object and use that to create an empty text file?

  2. #2
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: command line arguments function

    It would help if you posted some code here, I'm not sure exactly what's going on.
    Why are you using argc/*argv[]?
    So basically you just need the function to create txt files with those names that are passed?

  3. #3
    Join Date
    Feb 2010
    Posts
    6

    Re: command line arguments function

    The reason I'm using the argc, etc. is when I invoke the program:
    ./MyProgram.exe xyz
    where xyz is stored into the argv


    This is the original instruction in the assignment:
    Code:
    You will execute your program as
    
    ./hw2.exe xyz
    
    where xyz is the prefix for your files.
    
    The driver will call a function with signature
    
    void create_names(int argc, char* argv[], string& iFileName,
        string& oFileNameAll, string& oFileNameGood, string& oFileNameSmall,
        string& oFileNameRepeat);
    
    to create the names for all of the files.
    
    If the input name is xyz, the input will be xyz.txt.
     Build the output names as xyz_output_all.txt, xyz_output_good.txt, 
    xyz_output_small.txt and xyz_output_repeat.txt.



    This is what's in the main function:

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    #include "create_names.h"
    #include "PlayerDatabase.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
       {
    
         ifstream inFile;
         ofstream outFileAll;
         ofstream outFileGood;
         ofstream outFileSmall;
         ofstream outFileRepeat;
    
         string inFileName;
         string outFileNameAll;
         string outFileNameGood;
         string outFileNameSmall;
         string outFileNameRepeat;
    
         PlayerDatabase db;
       
         create_names(argc, argv, inFileName, outFileNameAll, outFileNameGood,
    		  outFileNameSmall, outFileNameRepeat);
    
         inFile.open(inFileName.c_str());
         if (!inFile)
           {
    	 cerr << "Unable to open player data file" << endl;;
             exit(1);
           }
         cout << "Loading player data..." << endl << endl;
         db.load_team(inFile);
         inFile.close();
    
         cout << endl << "Printing player data for full team..." << endl << endl;
         outFileAll.open(outFileNameAll.c_str());
         db.print_team(outFileAll);
         outFileAll.close();   
         cout << "Done full team: "<< db.get_team_count() << " members" << endl;
             
         // there are three ways to invoke the copy constructor
         // 1. using an initializer, as below
         // 2. another form of initialization: PlayerDatabase db2 = db1;
         // 3. call or return by value - see 241 sample programs
    
         cout << endl << "Printing player data for good team..." << endl << endl;
         PlayerDatabase db2(db);
         outFileGood.open(outFileNameGood.c_str());
         db2.create_good_team();
         db2.print_team(outFileGood);
         outFileGood.close();
         cout << "Done good team: "<< db2.get_team_count() << " members" << endl;
         
         // ... and this will invoke the overloaded assignment operator
        
         cout << endl << "Printing player data for small team..." << endl << endl;
         PlayerDatabase db3;
         db3 = db;
    
         outFileSmall.open(outFileNameSmall.c_str());
         db3.create_small_team();
         db3.print_team(outFileSmall);
         outFileSmall.close();
         cout << "Done small team: "<< db3.get_team_count() << " members" << endl;
    
         cout << endl << "Printing player data for original team again..." << endl << endl;
         outFileRepeat.open(outFileNameRepeat.c_str());
         db.print_team(outFileRepeat);
         outFileRepeat.close();
         cout << "Done original team: "<< db.get_team_count() << " members" << endl;
    
    
             
         return 0;
         }

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: command line arguments function

    This is a simple exercise in string manipulation. Are you familiar with std::string's functionality and operators? If not, read up on them first.

  5. #5
    Join Date
    Feb 2010
    Posts
    6

    Re: command line arguments function

    Quote Originally Posted by Lindley View Post
    This is a simple exercise in string manipulation. Are you familiar with std::string's functionality and operators? If not, read up on them first.
    Alright, but before I go, could you please explain what the create_names function is doing, specifically, the purpose of the string&? This is not suppose to be hard, but when I attempt to begin understand how to incorporate the arguments into code, I'm getting confused.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: command line arguments function

    You're supposed to take in the "base name" specified on the command line, and then build a number of variations upon it, and store them in the output string parameters for use later in the program.

  7. #7
    Join Date
    Feb 2010
    Posts
    6

    Re: command line arguments function

    Quote Originally Posted by Lindley View Post
    You're supposed to take in the "base name" specified on the command line, and then build a number of variations upon it, and store them in the output string parameters for use later in the program.
    So, would this be correct then? I basically am using the parameter on the command line as a prefix for the file names by using the "+" operator.

    Code:
      iFileName = argv[1]; 
       // store the parameter (input file) from the command line
       
      //   BUILDING THE OUTPUT FILES
      //  ---------------------------  
          
    	oFileNameAll    = iFileName + "_output_all.txt";
    	 // will result in xyz_output_all.txt  
    	 
        oFileNameGood   = iFileName +"_output_good.txt";
    	 // will result in xyz_output_good.txt
    	 
    	oFileNameSmall  = iFileName +"_output_small.txt";
    	 // will result in xyz_output_small.txt
    	
    	oFileNameRepeat = iFileName +"_output_repeat.txt";
    	 // will result in xyz_output_repeat.txt  	 
    }
    In the assignment, it mentions I can use the overloaded "+" operator to concatenate two strings. Is that meaning that I need to make an operator+ function or is what I have so far what it's explaining?
    Last edited by JMac89; February 23rd, 2010 at 01:02 AM.

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: command line arguments function

    You should first store the command line parameter in a std::string variable. E.g.
    Code:
    std::string basename = argv[1];
    Then you can use the predefined operator+ for std::sting to do concatenations. No need to define your own operator.

    iFileName should be basename with ".txt" appended, oFileNameGood with "_output_good.txt", etc.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: command line arguments function

    If you add the .txt to iFileName after creating all the output names, then what you have is essentially correct. You'd want to verify that by breakpointing the debugger after the function call, and ensuring that all output strings are as expected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured