How to use command line argument in function calls and parameters?

I am trying to read in the arguments from the main function. My problem is how to get each argument in order to put them into functions. I know I have to use something like strcmp( argv[ currentArg ], "albus" ) == 0, but I am pretty confused on how to do it especially since there might be different numbers of arguments. For example:

./cnp cut 13 5 copy 33 7 paste 1 input-b.txt output.txt

cnp is the name of the main file the cut copy and paste keywords are the function in the program The numbers are the indices where the function has to cut, past and copy input-b.txt output.txt are the files.

Here, we're asking it to cut 5 characters starting at column 13. This should cut the 3-digit number after each name. Then, we ask it to make a copy of what's in columns 33 to 39; that's 7 characters starting in column 33, and it should give us a copy the last floating point number on each line. Then, we tell it to paste that at the start of the line, giving us another copy of these numbers at the start of each line. So the original would look like this

       Young  003   3  89.81  67.10  80.85  D
       Venus  002   8  72.29  73.59  76.20  A
      Jasmin  003   6  55.19  50.51  63.88  F
     Micheal  001   3  98.93  91.37  99.00  C
       Abram  001   2  50.23  90.14  57.36  E
   Rigoberto  002   8  61.63  94.64  77.05  B
         Noe  003   2  68.41  61.79  64.60  A
     Kristin  002   5  77.34  84.68  65.16  B
     Phillip  001   6  63.19  76.08  52.39  B
     Monique  001   6  81.76  57.62  80.15  A
       Verda  002  10  93.03  56.21  93.58  C
      Louise  003   2  70.30  71.37  61.91  C
       Vilma  001   9  71.09  93.43  76.72  G 

After the program runs, it would look like this:

80.85         Young   3  89.81  67.10  80.85  D
76.20         Venus   8  72.29  73.59  76.20  A
63.88        Jasmin   6  55.19  50.51  63.88  F
99.00       Micheal   3  98.93  91.37  99.00  C
57.36         Abram   2  50.23  90.14  57.36  E
77.05     Rigoberto   8  61.63  94.64  77.05  B
64.60           Noe   2  68.41  61.79  64.60  A
65.16       Kristin   5  77.34  84.68  65.16  B
52.39       Phillip   6  63.19  76.08  52.39  B
80.15       Monique   6  81.76  57.62  80.15  A
93.58         Verda  10  93.03  56.21  93.58  C
61.91        Louise   2  70.30  71.37  61.91  C
76.72         Vilma   9  71.09  93.43  76.72  G
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "document.h"

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

  char 



   if(argc > 0) {


   }

   else {

}

   return EXIT_SUCCESS;
}

The paste, cut, copy functions look like:

bool cut( char *line, int start, int n )
bool copy(char *line, int start, int n) 
bool paste(char *line, int start)

To read and print it out, we have these functions:

int readDocument( FILE *fp, char doc[ MAX_LINES ][ MAX_LENGTH + 1 ] )
void printDocument(FILE *fp, char doc[MAX_LINES][MAX_LENGTH + 1], int lines)

I have already completed theses functions but I need help on what to do in the main function on how to use the command line arguments. I am using a Linux machine.

2 answers

  • answered 2019-03-13 18:06 Esdras Xavier

    When you pass some value by param in C/C++ such as:

    ./cnp cut 13 5 copy 33 7 paste 1 input-b.txt output.txt, you need use argv[positon_of_your_param], so your argv[0] will be the same as ./cnp, and your argv[1] will be cut.

    Now following this you can just use each one based in your argument, try the code bellow I made it in C, but would be almost the same to C++. All your argv return char, so if you wanna get a integer you must convert, you can use atoi(), this function get a char and return a integer doc.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void error(char *c, int n) {
      printf("%s\n", c);
      exit(n);
    }
    
    int main(int argc, char *argv[]) {
    
      char *firstAction, *secondAction, *thirdAction;
      char *file1, *file2;
      int howManyCharacters=0, startAt=0;
      int columnStart=0, columnEnd=0;
      int nAfterPaste = 0;
    
      if (argc < 10) {
        error("You must specify 10 parameters at least", -1);
      } else {
        firstAction       = argv[1];
        startAt           = atoi(argv[2]); // atoi(const char * str) -> This function convert your char into integer
        howManyCharacters = atoi(argv[3]);
        secondAction      = argv[4];
        columnStart       = atoi(argv[5]);
        columnEnd         = atoi(argv[6]) + columnStart;
        thirdAction       = argv[7];
        nAfterPaste       = atoi(argv[8]);
        file1             = argv[9];
        file2             = argv[10];
    
        printf("%s ", firstAction);
        printf("%i ", startAt);
        printf("%i ", howManyCharacters);
        printf("%s ", secondAction);
        printf("%i ", columnStart);
        printf("%i ", columnEnd);
        printf("%s ", thirdAction);
        printf("%i ", nAfterPaste);
        printf("%s ", file1);
        printf("%s \n", file2);
    
      }
    
      return EXIT_SUCCESS;
    }
    

  • answered 2019-03-13 19:05 JeremyP

    You have an array of strings in argv that looks like this:

    { "./cnp", "cut", "13", "5", "copy", "33", "7", "paste", "1", "input-b.txt", "output.txt" }
    

    On the assumption that the last two arguments are always the input and output files, I would loop through the arguments from 1 to arc - 3 to process the commands e.g. the below sketch is not a complete solution but should give you some ideas.

    if (argc < 3) 
    {
        // Handle the fact that there were not enough arguments.
    }
    else 
    {
        int commandArgs = argc - 2;
        for (int i = 1 ; i < commandArgs ; ++i) // Start at 1 to omit program name
        {
            if (strcmp(argv[i], "cut") == 0) 
            {
                if (commandArgs - i < 2)
                {
                    // Handle not enough args to cut
                }
                else 
                {
                    // You have a cut command, call cut or save it to call cut later       
                    i += 2; // Skip the two parameters 
                }
            }
            else if (strcmp(argv[i], "copy") == 0)
            {
                // similar pattern to above
            }
            else if (strcmp(argv[i], "paste") == 0)
            {
                // similar pattern to above
            }  
            else 
            {
                // Handle invalid command error
            }          
        }
    }