Command line arguements in functions.

This is a discussion on Command line arguements in functions. within the C++ Programming forums, part of the General Programming Boards category; Alright, I don't have any code for this one cause I've yet to start it, I just want to clear ...

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062

    Command line arguements in functions.

    Alright, I don't have any code for this one cause I've yet to start it, I just want to clear a few things up before I figure out how I do it. I'll note now that I'm struggling with how to explain this and I may end up repeating the same points or droning on and on about nothing. Be warned.

    As with my last command line post, this is dealing with files, but now I want to add some functions that allow you to manipulate parts of files, such as replacing words, cutting them down to a certain size (by the way, I may have later questions in this as I want people to be able to cut down files by number of bytes if they wish), and other things to manipulate them.

    The thing I'm not sure about is, would I be allowed to pass all command line arguements to a function in a function call and how would it be done. Basically, the main program will decide what the person is trying to do up until argv[1], then it will pass the action of how to handle it to the function.

    ...but in the function, I may need to send the rest of the command line arguements to it so it can handle them appropriately. Let me give an example of user input:
    Code:
    C:\ > fileManip replace "January 15th" "Febuary 12th" all report.txt
    
    Where the format would this is:
    fileManip replace <word(s) to replace> <word(s) to replace with> <starting instance(or all)> 
    <ending instance(if not all)> <filename or pathname>
    That's a lot of aguements being passed. The way I want the program to handle it is in main() have a if statement for argv[1] and depending what that is, call the appropriate function and pass the arguements to it. Now, what I'm hoping you're gonna tell me is this:
    Code:
     if (strcmp(argv[1], "replace") == 0)
        replace(argv[]);    // The function call passing the arguements
    Is it that simple to pass the arguements with no problems?

    Alright... I'll stop here with that question. I have more but I'll give you this one first and maybe post the rest in replies.
    Last edited by SlyMaelstrom; 10-30-2005 at 06:29 AM.
    Sent from my iPadŽ

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    This is the correct syntax. If you are passing all of argv to a function then you will probably want to also pass argc so that the function will know how many arguments are in the array. Never "assume" the arguments or number of arguments are correct.
    Code:
     if ( strcmp(argv[1], "replace") == 0)
        replace(argc, argv);    // The function call passing the arguements

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    OK, thanks. So it was simple. I'll edit this post with a question in a bit.
    Sent from my iPadŽ

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can pass a slice of argv (or any array) simply by ignoring the first number of indices that you want remove. Do that by subscripting the first index that you want to keep, and then pass its address:
    Code:
    #include <iostream>
    
    void foo ( char *a[] )
    {
      while ( *a != NULL )
        std::cout<< *a++ <<'\n';
    }
    
    int main ( int argc, char *argv[] )
    {
      if ( argc >= 2 )
        foo ( &argv[2] );
    }
    My best code is written with the delete key.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Great answer, thank you.

    By the way, if anyone was curious, here is a prototype of what this will be able to do. All of it is subject to change, but if you have any opinions, critiques are welcome.

    Code:
    Function Name   Description                        Format
    
    replace()       Replace a string in a              replace <word(s) to replace> <word(s) to replace with> <starting
                    file with another string           instance(or all)> <ending instance(if not all)> <filename or pathname> 
                                                       <save to>
    
    fileCat()       Merges two files together          merge <file 1> <file 2> <merge type*> <save to>
    
    count()         Counts the number of units         count <unit*> <filename>
                    in a file
    
    setSize()       Changes the size of a file by      setsize <size> <unit*> <filename>
                    unit
    
    compare()       Compares the similiarites of       compare <file 1> <file 2>
                    two files by words
    
    format()        Formats the structural apperance   format <format option*> <suboption*> <filename>
                    of a file
    
    excelFormat()   Excel has a specific format that   excelFormat <filename>
                    a txt file must be in to upload
                    to a spreadsheet. This formats
                    the file for you.
    
    
    Merge Types can do: One after the next, alternating words, alternating paragraphs
    
    Units are: letters, words, paragraphs, bytes
    
    Format Options are: Line Spacing, Capitalization (if I can, I'd like it to be able to capitalize words
                        at the beginning of sentences), puntuation (Again, this would be tough, but I can thinl
                        of a few standard rules it can check for), indentation, column setting
    
    Sub Options are: for Line Spacing (Single, Double, double between paragraphs) for column setting (width between)
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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