大佬教程收集整理的这篇文章主要介绍了c – 自定义命令行参数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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; }
您必须检查它们是否以特定字符串开头,例如使用strncmp,然后传递相关位的地址,例如argv [1] 8作为输入类型.
由于您提供了–input = type前缀,因此您可能还希望处理它们可能处于其他顺序的可能性.
例如,您可以替换:
int main (int argc,char* argv[]) { ifstream in(argv[1]); ofstream out(argv[2]);
有类似的东西:
int main (int argc,char* argv[]) { char *infile = 0; char *outfile = 0; for (int i = 1; i < argc; i++) { if (strncmp (argv[i],"--input=",8) == 0) { infile = argv[i] + 8; } else { if (strncmp (argv[i],"--output=",9) == 0) { outfile = argv[i] + 9; } else { std::cerr << "Invalid argument [" << argv[i] << "]" << std::endl; return -1; } } } if ((infile == 0) || (outfile == 0)) { std::cerr << "Need to specify input and output file" << std::endl; return -1; } ifstream in(infilE); ofstream out(outfilE);
以上是大佬教程为你收集整理的c – 自定义命令行参数全部内容,希望文章能够帮你解决c – 自定义命令行参数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。