Use C ++ command-line arguments

Use basic form of the command-line arguments
  • argc: Is the number of parameters 初始值为1.
    argv[]: The deposit is the first address of a command line parameter, the initial value from the 1start.

    as follows:

    The position of program execution argv[0]
    NULL argv[1]
  • Code

    #include <bits/stdc++.h>
    using namespace std;
    int main(int argc, char *argv[])
    {
    	//程序执行部分
    	return 0;
    }
    
Examples
  • Use cmdthe calling program, given the parameters, execute the program.
    cmd 可执行程序路径 参数1 参数2

  • Characters read out, and output.

  • 读取文件And 输出到文件when used ‘<', and ‘>’(note, not have the main function argvand argcinput parameters)

  • Code:

    #include <bits/stdc++.h>
    using namespace std;
    int main(int argc, char *argv[])
    {
    	for (int i = 1; i <= argc; i++)
    	{
    		cout << argv[i] << endl;
    	}
    	return 0;
    }
    
    
  • test:
    Here Insert Picture Description

Published 119 original articles · won praise 20 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_40727946/article/details/103539458