Results 1 to 2 of 2
I'M learning c++ but I'M doing so from a linux environment. I need to know how to receive command line arguments in C++. I know in bash you use $1, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-17-2013 #1Linux Newbie
- Join Date
- Dec 2010
- Posts
- 110
c++ and linux
I'M learning c++ but I'M doing so from a linux environment. I need to know how to receive command line arguments in C++. I know in bash you use $1, $2, $3, etc... . Thanks.
- 01-17-2013 #2
Since the ancient days of C, the command line arguments are passed through a couple magical variables available to main():
I hope this helps. argv[0] is typically the name of your program - which is not as silly as it might sound. Using that, you can often figure out where your program lives or you can do different things depending on how it gets called.Code:$ cat HelloWorld.cpp #include <iostream> using namespace std; int main(int argc, char **argv) { for (int i=0; i<argc; i++) cout << i << ": " << argv[i] << "\n"; return 0; } $ ./HelloWorld 'this is a' test 0: ./HelloWorld 1: this is a 2: test


1Likes
Reply With Quote
