Hi,
I only make console programs, and would like to load my program like this ./programme.o -var1 value -var2 value_two
So how can I add some parameters, and that they're put in variables when the program loads?
This is a discussion on Parameter? within the C++ Programming forums, part of the General Programming Boards category; Hi, I only make console programs, and would like to load my program like this ./programme.o -var1 value -var2 value_two ...
Hi,
I only make console programs, and would like to load my program like this ./programme.o -var1 value -var2 value_two
So how can I add some parameters, and that they're put in variables when the program loads?
Those are command line arguments. You have to add the code to parse those arguments in your program. The first step is to change your main function to look like this:The argc variable is the number of arguments. The argv variable is an array of C style strings holding each command line argument. The command line arguments are separated by spaces, unless they are in quotes. The first argument is always the program name. So for your example: ./programme.o -var1 value -var2 value_two argc would be 5 and the contents of argv would be:Code:int main(int argc, char* argv[])You just have to find a way to look at that data and make sense of it.Code:argv[0] -> "./programme.o" argv[1] -> "-var1" argv[2] -> "value" argv[3] -> "-var2" argv[4] -> "value_two" argv[5] -> NULL
Thank you, this works perfectlyOriginally Posted by Daved
![]()