argc 是 argument count的缩写,表示传入main函数的参数个数;
argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,并且第一个参数argv[0]一定是程序的名称,并且包含了程序所在的完整路径,所以确切的说需要我们输入的main函数的参数个数应该是argc-1个;
#include <iostream>
using namespace std;
void main(int argc,char *argv[])
{
for(int i=0;i<argc;i++)
{
cout<<"argument["<<i<<"] is: "<<argv[i]<<endl;
}
system("pause");
}
argv是指向指针的指针,main函数的第二个参数“char *argv[]“也可以替换为 “char **argv“,两者是等价的。
在没有参数传入的情况下,保存程序名称的第一个变量argv[0]依然存在,存的是文件的路径。
读取图片,利用argv
#include <iostream>
#include <core/core.hpp>
#include <highgui/highgui.hpp>
using namespace std;
using namespace cv;
void main(int argc,char **argv)
{
Mat image=imread(argv[1]);
imshow("Lena",image);
waitKey();
}
f_name.exe Lena.jpg
Reference : https://blog.csdn.net/dcrmg/article/details/51987413