Friday, November 27, 2009

Is It Possible To Abuse Oragel

Search this list command-line arguments in C

After long and exhausting struggle with nvcc (compiled code nvcc not much to be blended with g + +, I do not know why, other WORKS - SOA # 1) decided that swarm optimization algorithm will have the form of a program in C, which writes the result of the algorithm in the base sqlite3, and I myself in another application has already compiled a normal compiler will machinability data. In the meantime, it appeared that it would be nice to set the number of particle swarm, and I decided to use the command line arguments. Then came the problem of how to

argc and argv

draw is what I mean. This operation is used on the agenda in the programs, so there is a suitable procedure for this purpose, and it is called
getopt ()
and is located in the header file unistd.h

. At the moment I use two options:-n

and
-o
. Both require an argument. Here's the code:


unistd.h stdlib.h # include stdio.h
int main (int argc, char ** argv) {
/ / initialize variables
int N = 10, / / \u200b\u200bNumber of agents
 int c = 0; 
dbfilename char * = NULL;

while ((c = getopt (argc, argv, "n: o:"))! = -1) {

switch (c) {
< N) {
case 'n':
N = atoi(optarg);
printf("Number of particles: %d\n", N);
if (N
{
fprintf(stderr, "Invalid number of particles, setting to default = 10\n");
N = 10;
}
break;
case 'o':
dbfilename = optarg;
printf("Output to file: %s\n", dbfilename);
break; < N);
case '?':
switch(optopt)
{
case 'n':
fprintf (stderr, "Option-n sets number of particles and requires an integer argument. \\ N");
break;
case 'o':
fprintf (stderr, "Option-o sets name of output file and requires a string argument. \\ n ");
break;
default:
fprintf (stderr," Unknown option:% c \\ n ", optopt);
};
break;
default: } First after "przegryzieniu" by all the arguments getopt () returns -1, which is why such a condition is a while. Otherwise, get the value of the character that represents an option. Options to expect in the program serve as the third argument. To select that option argument is required to be added after the ": ." To select the option to add an optional argument ": . The switch, take the appropriate action depending on the options. For -n
will replace Alphanumeric is an integer, a replacement string to an integer, then checking if the value is greater than 0 The argument for options is stored in optarg
 
as a string.

-o option
is responsible for the output file name. As you can see, you can assign a pointer
optarg
to a char * pointer, ie it is not a const char * (as if that was the case for example printf ("Text type const char *").
More
optarg points to one of the elements
argv
, so you can modify it. In case of errors is given a value
'?'
which may mean missing argument for option or an unknown option.


default option from what I understand has no right to happen, but I added some code there to find out what happens in case. Finally, a good programmer looks both ways before crossing the one-way street.
<= 0)

0 comments:

Post a Comment