norman's blog

Notes of an amnesiac.
Never stop thinking.
Find an aesthetic description.

Sunday, May 31, 2009

argc&argv

Argument passed to main()
int argc;
argc is an abbreviation for "argument count". It is the traditional name for the first argument to a C program's main routine. By convention, it holds the number of arguments that are passed to main in the argument vector argv. Because argv[0] is always the name of the command, the value of argc is always one greater than the number of command-line arguments that the user enters.
char *argv[];
argv is an abbreviation for ``argument vector''. It is the traditional name for a pointer to an array of string pointers passed to a C program's main function; by convention, it is the second argument passed to main. By convention, argv[0] always points to the name of the command itself.
Example of argument reading codes
    if( argc == 1 )
    {
        printf( "Usage: %s\n  -data \n"
                "  -info \n"
                "  [-maxSizeDiff ]\n"
                "  [-maxPosDiff ]\n"
                "  [-sf ]\n"
                "  [-ni]\n"
                "  [-nos ]\n"
                "  [-rs ]\n"
                "  [-w ]\n"
                "  [-h ]\n",
                argv[0], maxSizeDiff, maxPosDiff, scale_factor, nos, rocsize,
                width, height );
        
        return 0;
    }

    for( i = 1; i < argc; i++ )
    {
        if( !strcmp( argv[i], "-data" ) )
        {
            classifierdir = argv[++i];
        }
        else if( !strcmp( argv[i], "-info" ) )
        {
            infoname = argv[++i];
        }
        else if( !strcmp( argv[i], "-maxSizeDiff" ) )
        {
            maxSizeDiff = (float) atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-maxPosDiff" ) )
        {
            maxPosDiff = (float) atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-sf" ) )
        {
            scale_factor = atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-ni" ) )
        {
            saveDetected = 0;
        }
        else if( !strcmp( argv[i], "-nos" ) )
        {
            nos = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-rs" ) )
        {
            rocsize = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-w" ) )
        {
            width = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-h" ) )
        {
            height = atoi( argv[++i] );
        }
    }

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home