传递命令行参数 [英] Passing command line arguments
问题描述
好吧,我更新了我的代码一点,但我仍然不知道如何使用我传递的命令行参数的向量。我试图设置它像我下面的代码,但它不会编译。它给我的错误,它找不到argc和argv:
1> c:\users\chris\documents\visual studio 2008\projects\\ \\ cplusplustwo\cplusplustwo\application.h(32):error C2065:'argc':undeclared identifier
1> c:\users\chris\documents\visual studio 2008\projects\ cplusplustwo\cplusplustwo\application.h(32):error C2065:'argv':undeclared identifier
main.cpp
#includeapplication.h
int main(int argc,char * argv []){
vector< string& args(argv,argv + argc);
return app.run(args);
}
application.h
#include< boost / regex.hpp>
#include< iostream>
#include< string>
#include< fstream>
#include< sstream>
#includetime.h
using namespace std;
类应用程序{
private:
//变量
boost :: regex expression;
string line;
string pat;
string replace;
int lineNumber;
char date [9];
char time [9];
void commandLine(vector< string> args){
string expression =; // Expression
string textReplace =; // Replacement Text
string inputFile =; //输入文件
string outputFile =; //输出目录
int optind = 1;
//解码参数
for(vector< string> :: iterator i = args.begin(); i!= args.end(); ++ i){
while optindstring sw = argv [optind];
if(* i ==-e){
optind ++;
expression = argv [optind];
}
else if(* i ==-t){
optind ++;
textReplace = argv [optind];
}
else if(* i ==-i){
optind ++;
inputFile = argv [optind];
}
else if(* i ==-o){
optind ++;
outputFile = argv [optind];
}
else {
cout<< 未知开关:
<< argv [optind]< 请输入正确的参数之一:\\\
<< -e + \表达式\\\\
t + \替换文本\\\\
i + \输入文件\\\\
o + \输入文件\\\\
;
optind ++;
}
}
}
}
//函数
void getExpression(){
cout< Expression:;
getline(cin,pat);
try {
expression = pat;
}
catch(boost :: bad_expression){
cout<< pat<< 不是有效的正则表达式\\\
;
exit(1);
}
}
void boostMatch(){
//定义替换{仅用于测试目的!提交之前提交!
replace =;
_strdate_s(date);
_strtime_s(time);
lineNumber = 0;
//要打开的文件
//输入文件
ifstream in(files / trff292010.csv);
if(!in)cerr<< no file\\\
;
//输出文件
ofstream newFile(files / NEWtrff292010.csv);
ofstream copy(files / ORIGtrff292010.csv);
ofstream report(files / REPORT.dat,ios.app);
lineNumber ++;
while(getline(in,line)){
lineNumber ++;
boost :: smatch matches;
copy<<线<< '\\\
';
if(regex_search(line,matches,expression)){
for(int i = 0; i< matches.size(); ++ i){
report< 时间:<时间< Date:<<日期<< '\\\
'
<< Line<< lineNumber<<:<<线<< '\\\
';
newFile<< boost :: regex_replace(line,expression,replace)<< \\\
;
}
} else {
newFile<<线<< '\\\
';
}
}
}
public:
void run(vector< string> args){
commandLine(vector< string& );
getExpression();
boostMatch();
}
};
ORIGINAL POST
我想通过命令行参数。这是高级C ++类的家庭作业。我需要传递一个向量的命令行,我不知道如果我做的一切正确。我会把它传入一个像我一样的向量吗?还有一个copy()命令,你可以使用将命令行参数复制到向量而不是推后?
main。 cpp
#includeapplication.h
int main(int argc,char * argv []){
vector< string& args;
应用程序应用程序;
for(int i = 1; iargs.push_back(argv [i]);
}
app.run(args);
return(0);
}
application.h
#include< boost / regex.hpp>
#include< iostream>
#include< string>
#include< fstream>
#include< sstream>
#includetime.h
using namespace std;
类应用程序{
private:
//变量
boost :: regex expression;
string line;
string pat;
string replace;
int lineNumber;
char date [9]
char time [9];
void commandLine(vector< string> args){
string expression =; // Expression
string textReplace =; // Replacement Text
string inputFile =; //输入文件
string outputFile =; //输出目录
int optind = 1;
//解码参数
for(vector< string> :: iterator i = args.begin(); i!= args.end(); ++ i){
while optindstring sw = argv [optind];
if(* i ==-e){
optind ++;
expression = argv [optind];
}
else if(* i ==-t){
optind ++;
textReplace = argv [optind];
}
else if(* i ==-i){
optind ++;
inputFile = argv [optind];
}
else if(* i ==-o){
optind ++;
outputFile = argv [optind];
}
else {
cout<< 未知开关:
<< argv [optind]< 请输入正确的参数之一:\\\
<< -e + \表达式\\\\
t + \替换文本\\\\
i + \输入文件\\\\
o + \输入文件\\\\
;
optind ++;
}
}
}
}
//函数
void getExpression(){
cout< Expression:;
getline(cin,pat);
try {
expression = pat;
}
catch(boost :: bad_expression){
cout<< pat<< 不是有效的正则表达式\\\
;
exit(1);
}
}
void boostMatch(){
//定义替换{仅用于测试目的!提交之前提交!
replace =;
_strdate_s(date);
_strtime_s(time);
lineNumber = 0;
//要打开的文件
//输入文件
ifstream in(files / trff292010.csv);
if(!in)cerr<< no file\\\
;
//输出文件
ofstream newFile(files / NEWtrff292010.csv);
ofstream copy(files / ORIGtrff292010.csv);
ofstream report(files / REPORT.dat,ios.app);
lineNumber ++;
while(getline(in,line)){
lineNumber ++;
boost :: smatch matches;
copy<<线<< '\\\
';
if(regex_search(line,matches,expression)){
for(int i = 0; i< matches.size(); ++ i){
report< 时间:<时间< Date:<<日期<< '\\\
'
<< Line<< lineNumber<<:<<线<< '\\\
';
newFile<< boost :: regex_replace(line,expression,replace)<< \\\
;
}
} else {
newFile<<线<< '\\\
';
}
}
}
public:
void run(vector< string> args){
commandLine(vector< string& );
getExpression();
boostMatch();
}
};
解决方案我只需写:
矢量< string> args(argv + 1,argv + argc +!argc);
这将排除
argv [0]
即使argc == 0
(可能在Linux下,也可能是其他操作系统)。Okay I have updated my code a little, but I am still not exactly sure how to use the vector of command line arguments that I pass. I tried to set it up like the code I have below, but it wont compile. It gives me the error that it cannot find argc and argv:
1>c:\users\chris\documents\visual studio 2008\projects\cplusplustwo\cplusplustwo\application.h(32) : error C2065: 'argc' : undeclared identifier 1>c:\users\chris\documents\visual studio 2008\projects\cplusplustwo\cplusplustwo\application.h(32) : error C2065: 'argv' : undeclared identifier
main.cpp
#include "application.h" int main(int argc,char *argv[]){ vector<string> args(argv, argv + argc); return app.run(args); }
application.h
#include <boost/regex.hpp> #include <iostream> #include <string> #include <fstream> #include <sstream> #include "time.h" using namespace std; class application{ private: //Variables boost::regex expression; string line; string pat; string replace; int lineNumber; char date[9]; char time[9]; void commandLine(vector<string> args){ string expression=""; // Expression string textReplace=""; // Replacement Text string inputFile=""; // Input File string outputFile=""; // Output Directory int optind=1; // decode arguments for(vector<string>::iterator i = args.begin(); i != args.end(); ++i){ while ((optind < argc) && (argv[optind][0]=='-')) { string sw = argv[optind]; if (*i == "-e") { optind++; expression = argv[optind]; } else if (*i == "-t") { optind++; textReplace = argv[optind]; } else if (*i == "-i") { optind++; inputFile = argv[optind]; } else if (*i == "-o") { optind++; outputFile = argv[optind]; } else{ cout << "Unknown switch: " << argv[optind] << "Please enter one of the correct parameters:\n" << "-e + \"expression\"\n-t + \"replacement Text\"\n-i + \"Input File\"\n-o + \"Onput File\"\n"; optind++; } } } } //Functions void getExpression(){ cout << "Expression: "; getline(cin,pat); try{ expression = pat; } catch(boost::bad_expression){ cout << pat << " is not a valid regular expression\n"; exit(1); } } void boostMatch(){ //Define replace {FOR TESTING PURPOSES ONLY!!! REMOVE BEFORE SUBMITTING!! replace = ""; _strdate_s(date); _strtime_s(time); lineNumber = 0; //Files to open //Input Files ifstream in("files/trff292010.csv"); if(!in) cerr << "no file\n"; //Output Files ofstream newFile("files/NEWtrff292010.csv"); ofstream copy("files/ORIGtrff292010.csv"); ofstream report("files/REPORT.dat", ios.app); lineNumber++; while(getline(in,line)){ lineNumber++; boost::smatch matches; copy << line << '\n'; if (regex_search(line, matches, expression)){ for (int i = 0; i<matches.size(); ++i){ report << "Time: " << time << "Date: " << date << '\n' << "Line " << lineNumber <<": " << line << '\n'; newFile << boost::regex_replace(line, expression, replace) << "\n"; } }else{ newFile << line << '\n'; } } } public: void run(vector<string> args){ commandLine(vector<string> args); getExpression(); boostMatch(); } };
ORIGINAL POST
I want to pass command line arguments out of main. This is homework for an advanced C++ class. I need to pass the command line with a vector and I am not sure if I am doing everything correctly. Would I pass it into a vector like I did? Also is there a copy() command you can use to copy the command line arguments into a vector rather than pushback?
main.cpp #include "application.h" int main(int argc,char *argv[]){ vector<string> args; application app; for (int i=1;i<argc;i++){ args.push_back(argv[i]); } app.run(args); return(0); }
application.h
#include <boost/regex.hpp> #include <iostream> #include <string> #include <fstream> #include <sstream> #include "time.h" using namespace std; class application{ private: //Variables boost::regex expression; string line; string pat; string replace; int lineNumber; char date[9]; char time[9]; void commandLine(vector<string> args){ string expression=""; // Expression string textReplace=""; // Replacement Text string inputFile=""; // Input File string outputFile=""; // Output Directory int optind=1; // decode arguments for(vector<string>::iterator i = args.begin(); i != args.end(); ++i){ while ((optind < argc) && (argv[optind][0]=='-')) { string sw = argv[optind]; if (*i == "-e") { optind++; expression = argv[optind]; } else if (*i == "-t") { optind++; textReplace = argv[optind]; } else if (*i == "-i") { optind++; inputFile = argv[optind]; } else if (*i == "-o") { optind++; outputFile = argv[optind]; } else{ cout << "Unknown switch: " << argv[optind] << "Please enter one of the correct parameters:\n" << "-e + \"expression\"\n-t + \"replacement Text\"\n-i + \"Input File\"\n-o + \"Onput File\"\n"; optind++; } } } } //Functions void getExpression(){ cout << "Expression: "; getline(cin,pat); try{ expression = pat; } catch(boost::bad_expression){ cout << pat << " is not a valid regular expression\n"; exit(1); } } void boostMatch(){ //Define replace {FOR TESTING PURPOSES ONLY!!! REMOVE BEFORE SUBMITTING!! replace = ""; _strdate_s(date); _strtime_s(time); lineNumber = 0; //Files to open //Input Files ifstream in("files/trff292010.csv"); if(!in) cerr << "no file\n"; //Output Files ofstream newFile("files/NEWtrff292010.csv"); ofstream copy("files/ORIGtrff292010.csv"); ofstream report("files/REPORT.dat", ios.app); lineNumber++; while(getline(in,line)){ lineNumber++; boost::smatch matches; copy << line << '\n'; if (regex_search(line, matches, expression)){ for (int i = 0; i<matches.size(); ++i){ report << "Time: " << time << "Date: " << date << '\n' << "Line " << lineNumber <<": " << line << '\n'; newFile << boost::regex_replace(line, expression, replace) << "\n"; } }else{ newFile << line << '\n'; } } } public: void run(vector<string> args){ commandLine(vector<string> args); getExpression(); boostMatch(); } };
解决方案I'd just write:
vector<string> args(argv + 1, argv + argc + !argc);
This will exclude
argv[0]
, but in a way that's robust even ifargc == 0
(possible under Linux, and maybe other OSs too).这篇关于传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!