How to set up command line in C++?

How to set up command line in C++?

In C++, you can use command line parameters to pass parameters and options that the program needs when running. In the main function, you can get the command line parameters through two parameters, they are argc and argv. argc represents the number of command line parameters, including the program name. argv is a pointer array that contains argc strings, each string representing a command line parameter. Among them, argv[0] represents the program name, argv[1] represents the first parameter, argv[2] represents the second parameter, and so on. Here is a simple example code demonstrating how to use command line parameters: #include

int main(int argc, char* argv[])
{ std::cout << "argc = " << argc << std::endl; for (int i = 0; i < argc; i++) { std:: cout << “argv[” << i << "] = " << argv[i] << std::endl; } return 0; } Enter the program name and parameters on the command line, for example: ./myprogram arg1 arg2 arg3 After running the program, the following results will be output: argc = 4 argv[0] = ./myprogram argv[1] = arg1 argv[2] = arg2 argv[3] = arg3 In actual use, the command can be parsed as needed Line parameters and perform different operations based on the parameters. Private enterprise cloud disk coded in C++













Guess you like

Origin blog.csdn.net/Yuku226/article/details/132739886