|
-
Aug 20th, 2001, 08:40 PM
#1
Thread Starter
Fanatic Member
Program arguments
How can I pass arguments to a console program? Something like: progname -a
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 20th, 2001, 08:45 PM
#2
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 21st, 2001, 12:22 PM
#3
Thread Starter
Fanatic Member
Ok, that helped a lot. I wrote a little program using it, but it crashes.
Code:
#include <iostream.h>
#include <string.h>
int main(int argc, char* argv[])
{
for (int i = 1; i <= argc; i++)
{
if (strcmp(argv[i], "-a") == 0)
cout<<"ok"<<endl;
}
return 0;
}
If i type "<progname> -a" it prints ok, but it still crashes.
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 21st, 2001, 12:25 PM
#4
Member
How does it crash? Does it ever get to the return 0?
-
Aug 21st, 2001, 12:31 PM
#5
Dont know if this is the problem, but change your for loop to this:
Code:
for(int i = 0; i < argc; ++i) {.. }
Z.
-
Aug 21st, 2001, 12:31 PM
#6
Frenzied Member
USe
Code:
#include <iostream.h>
#include <string.h>
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-a") == 0)
cout<<"ok"<<endl;
}
return 0;
}
-
Aug 21st, 2001, 12:31 PM
#7
-
Aug 21st, 2001, 01:18 PM
#8
Monday Morning Lunatic
Also remember that argv[argc] is defined (in the standard) to be NULL
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|