C++主函数参数
学习C++主函数的参数输入,用于从command line中读取参数,下面以读取视频文件为例进行说明
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "usage: play_video <video_path>" << std::endl;
return -1;
}
std::string video_path = argv[1];
// check video file exist or not
std::ifstream video_file(video_path);
if (!video_file.is_open()) { // 同video_file.fail()
std::cerr << "Video file not exist!" << std::endl;
return -1;
}
// VideoCapture
cv::VideoCapture cap(video_path);
if (!cap.isOpened()) {
std::cerr << "Open Video Failed!" << std::endl;
return -1;
}
// Video Processing code here
cv::Mat frame;
while(1) {
cap >> frame;
if(frame.empty())
break;
cv::imshow("frame", frame);
cv::waitKey(25);
}
return 0;
}
- Argparse for modern C++: https://github.com/p-ranav/argparse
- Lightweight C++ command line option parser: https://github.com/jarro2783/cxxopts

浙公网安备 33010602011771号