parsing arguments

H

Harris Kosmidis

Hello, I'm writing a program and I'm stuck in something silly, which I
thought it worked for some time.

I want to parse the arguments.
I want to run it like:
../prog image.jpg [-t] [-r floatnum] [-n floatnum]

Here's the code:

char fname[100];
double f_radius=0.8,f_n=0.8;
while (--argc) {
++argv;
if (*argv[0] != '-')
strncpy (fname, argv[0], 99);
else
++argv[0];

switch (*argv[0]) {
case 'd': debug = 1; break;
case 'r': ++argv; --argc;f_radius=atof(argv[0]);break;
case 'n': ++argv; --argc;f_n=atof(argv[0]);break;
case 't': ++argv; --argc;train=1;break;
}
}

If I do:
../a.out testimage.jpg -t -r 0.09 -n 0.09
it goes OK.

When I do:
../a.out testimage.jpg -r 0.09 -n 0.09 [omitting -t that is]
I get
main.c main 102 unable to open image `0.09': No such file or directory

(error that ImageMagick can't open the image file I provided).

What's wrong? Can somebody help me out?

thanks
 
A

Andrew Poelstra

Hello, I'm writing a program and I'm stuck in something silly, which I
thought it worked for some time.

I want to parse the arguments.
I want to run it like:
./prog image.jpg [-t] [-r floatnum] [-n floatnum]

Here's the code:

char fname[100];
double f_radius=0.8,f_n=0.8;
while (--argc) {
++argv;
if (*argv[0] != '-')
strncpy (fname, argv[0], 99);
else
++argv[0];

Should this be ++argv?
switch (*argv[0]) {
case 'd': debug = 1; break;
case 'r': ++argv; --argc;f_radius=atof(argv[0]);break;
case 'n': ++argv; --argc;f_n=atof(argv[0]);break;
case 't': ++argv; --argc;train=1;break;

It looks here like you're treating `t' as a flag with a parameter,
but you aren't using the parameter so it's being thrown away.

Eliminate the ++argv; --argc; (so that it looks more like -d), and
your logic problem (if any is left) should be clear.
}
}

If I do:
./a.out testimage.jpg -t -r 0.09 -n 0.09
it goes OK.

When I do:
./a.out testimage.jpg -r 0.09 -n 0.09 [omitting -t that is]
I get
main.c main 102 unable to open image `0.09': No such file or directory

(error that ImageMagick can't open the image file I provided).

What's wrong? Can somebody help me out?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top