c++ - 自定义命令行参数

标签 c++ arguments customization

我对命令行参数有疑问。我完成了程序,所以我可以从命令行启动它:

program.exe test.txt copy_test.txt

基本上,我的程序执行以下操作:

  • 输入一些文本文件
  • 对其进行排序并复制到一个新的文本文件

但是(总是那样但是?!),我应该像这样从命令行启动程序:

program.exe -input=test.txt -output=copy_test.txt

我不知道该怎么做。我研究过,但没有找到任何帮助:(

请回复。

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main ( int argc, char* argv[])
{
 ifstream in(argv[1]);
 ofstream out(argv[2]);
 vector <string> sV;
 string line;
 while (in >> line)
  sV.push_back(line);
 for ( int i = 0; i < sV.size(); i++)
 sort ( sV.begin(), sV.end () );
 for ( int i = 0; i < sV.size(); i++)
 out << sV[i] << endl;
 cin.get();
 return 0;
}

最佳答案

你应该解析 mainargv参数以检查它们是否以 -input 开头, -output等等等等

从头开始做这件事很麻烦,但幸运的是有很多有用的库可以做到这一点,比如 boost.program_options

关于c++ - 自定义命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4780869/

相关文章:

c - 指针作为函数参数接收内存地址(C)

c++ - 如何找到分配动态内存的 C++ 语言结构?

c++ - std::map 迭代在第一次运行时出错

c++ - `IDispatch::GetTypeInfo()` 接口(interface)是怎么回事?

jquery - 使用 jquery 在预定义的 html block 中呈现自动完成建议

listview - JavaFX - 自定义 ListView 外观

delphi - 如何更改Delphi的 'Run'和 'Add Breakpoint'快捷键?

c++ - 有人知道一些可以帮助我学习的项目吗?

eclipse - 为什么 eclipse 为方法生成参数名称为 arg0,arg1,arg2....

c++ - 使用 pthread 时出现多个参数(带结构)错误