C++ 如何使用boost解析文件路径?
我使用boost::program\u选项和boost::filesystem。我需要解析命令行参数C++ 如何使用boost解析文件路径?,c++,boost,C++,Boost,我使用boost::program\u选项和boost::filesystem。我需要解析命令行参数 int main(int argc, char** argv) { //command line arguments variables_map vm; try{ options_description desc{ "Program usage " }; desc.add_options() ("help,h", "Show help
int main(int argc, char** argv)
{
//command line arguments
variables_map vm;
try{
options_description desc{ "Program usage " };
desc.add_options()
("help,h", "Show help")
("input,I", value<std::string>(), "Input directory")
("output,O", value<std::string>(), "Output file ");
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
int main(int argc,char**argv)
{
//命令行参数
变量映射vm;
试一试{
选项描述描述{“程序使用”};
说明添加选项()
(“帮助,h”,“显示帮助”)
(“输入,I”,value(),“输入目录”)
(“输出,O”,值(),“输出文件”);
存储(解析命令行(argc、argv、desc)、vm);
通知(vm);
如何将参数用作文件路径
path file_path = vm.count["input"];
path file_path = vm.count["input"].as<std::string>();
path file_path=vm.count[“input”];
路径文件_path=vm.count[“input”].as();
不工作您可以使用以下命令将命令行中提供的路径值分配到
文件路径
:
if (vm.count("input")) {
std::string my_path = vm["input"].as<std::string>();
boost::filesystem::path file_path (my_path);
std::cout << "The input path is: " << file_path << std::endl;
}
if(vm.count(“input”){
std::string my_path=vm[“input”].as();
boost::filesystem::path file\u path(my\u path);
std::cout除了使用两个赋值运算符时出现语法错误外,您所显示的代码还有什么问题?您确实尝试过,不是吗?在代码中指定使用名称空间boost::program_options;
。另外,desc()
需要圆括号,而不是卷曲括号。在代码中只有一个赋值。我不知道如何获取vm的值。count[“input”];错误:不允许键入名称。@MikeVasilenko请检查修订版本是否有效。在我的计算机上,它会产生正确的输出。