Showing posts with label External Arguments. Show all posts
Showing posts with label External Arguments. Show all posts

Thursday, July 26, 2012

External Arguments in ANSI C

The easiest way to send data to your program is to use external arguments. An external argument is a string that can be processed by your program.

Let us suppose that we have a console program called ArgPrinter. To execute the program and send external arguments you will need to open a console\terminal and type something like this:
ArgPrinter arg1 arg2 arg3 arg4
Arguments can also be sent by using other methods, but those are system specific (Example: When you create a shortcut in windows you can go to the shortcut's properties and put the arguments there).

If you want to access the arguments who were sent, your main function should have this prototype:
int main(int argc, char**argv)
argc is a integer value who represents the number of arguments who were sent.
argv is a array of strings containing each of the sent arguments plus the path to your executable file.

TIPYes, this means that if you send 4 arguments argc will be equal to 5 because of the path argument.

Now let's take an example implementation of the ArgPrinter program:
#include<stdio.h>;

int main(int argc, char** argv)
{
   int i;
   int returnValue = 0;
   if(argc>0)
   {
      printf("Number of arguments: %d\n", argc);
      for(i = 0; i<argc; i++)
      {
         printf("%s\n",argv[i]);
      }
   }
   else
   {
      returnValue = -1;
   }
   return returnValue;
}
/*Output
Number of arguments: 5
/home/dystopiancode/Ubuntu One/Eclipse/ArgPrinter/ArgPrinter
Arg1
Arg2
Arg3
Arg4
 */
The program above will print the number of arguments and after that it will print the value of each argument.

The value that your program returns can signify the execution status of the program. Usually programs return 0 if they were executed successfully. This feature is useful if you call a C program from a shell script or something similar.

Question: Is your C program called directly from console using the external arguments or it is called by another program?

References:
http://crasseux.com/books/ctutorial/argc-and-argv.html

Sunday, September 25, 2011

Reading Command Line Arguments in Java

In console applications, arguments sometimes play an important role. They may be used to set the program's behavior according to external (as in external to the program) variables. These external variables can be sent to the program as strings which the program interprets as a String vector.
public class CmdLineProgram 
{
    /*
     * Precondition:
     * The program knows the minimum and maximum number of arguments
     * which he allows 
     */
    public static void main(String[] args)
    {
        int minNrOfArgs = 0;    //Minimum number of arguments
        int maxNrOfArgs = 2;    //Maximum number of arguments
        try
        {
            if(args.length >= minNrOfArgs && args.length <=maxNrOfArgs)
            {
                if(args.length!=0)
                {
                    System.out.println("The arguments are: ");
                    for(String s: args)
                        System.out.println(s);
                }
                System.out.println("Total number of arguments: " 
                                  + args.length);
            }
            else
            {
                if(args.length <= minNrOfArgs)
                    throw new Exception("Number of arguments is "
                                        + args.length
                                        + ".\n" 
                                        + "At least "
                                        + minNrOfArgs
                                        + " are required.");
                else
                    throw new Exception("Number of arguments is "
                                        + args.length
                                        + ".\n" 
                                        +"At most "
                                        + minNrOfArgs
                                        + " are permitted.");
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }
}
What the program does:
Knowing the minimum and maximum number of arguments, the program checks if the length of the argument String vector (args) is between those values. If that is true, it will output the number of arguments and the value of each argument using a foreach loop (still, he will not output the arguments if he didn't receive any and will only show 0).
If the number of arguments is not correct (too many of too few) it will throw an exception and output that exception.
Related Posts Plugin for WordPress, Blogger...