CS 240: Data Structures Supplemental: Command Line Input
Commands When using the command line, some executables take “parameters” similarly to a function/method. When using the command line, some executables take “parameters” similarly to a function/method. g++ -c main.cpp g++ -c main.cpp cp main.cpp main.backup cp main.cpp main.backup
Command Line We can do this with our own files as well. We can do this with our own files as well. Instead of “int main()” Instead of “int main()” We can fill it in with parameters! Main can take input too! We can fill it in with parameters! Main can take input too! int main(int c, char **v) int main(int c, char **v) This is the standard convention This is the standard convention
Command Line int main(int c, char **v) int main(int c, char **v) What are c and v? What are c and v? c is number of parameters c is number of parameters v is a pointer to character strings v is a pointer to character strings In general, In general, v[0] == executable name v[0] == executable name v[1] == first parameter v[1] == first parameter v[n] == nth parameter v[n] == nth parameter
Command Line int main(int c, char **v) { for(int i=0;i<c;i++) { cout << v[i] <<endl; } return 0; } Use this code to see how command line input works. Use this code to see how command line input works. You may need to convert the character strings into something usable by your code. You may need to convert the character strings into something usable by your code.
Command Line - Alternate int main(int c, char **v) { vector commandline; for(int i=0;i<c;i++) { string temp = v[i]; commandline.push_back(temp);} return 0; }