[英]Passing command line arguments
好吧,我已經更新了我的代碼,但我仍然不確定如何使用我傳遞的命令行參數的向量。 我試着像我下面的代碼一樣設置它,但它不會編譯。 它給我一個錯誤,它無法找到argc和argv:
1> c:\\ users \\ chris \\ documents \\ visual studio 2008 \\ projects \\ cplusplustwo \\ cplusplustwo \\ application.h(32):錯誤C2065:'argc':未聲明的標識符1> c:\\ users \\ chris \\ documents \\ visual studio 2008 \\ projects \\ cplusplustwo \\ cplusplustwo \\ application.h(32):錯誤C2065:'argv':未聲明的標識符
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();
}
};
原始郵政
我想將命令行參數傳遞給main。 這是高級C ++類的功課。 我需要使用向量傳遞命令行,我不確定我是否正在做所有事情。 我會把它傳遞給像我一樣的矢量嗎? 還有一個copy()命令可以用來將命令行參數復制到向量而不是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();
}
};
我只想寫:
vector<string> args(argv + 1, argv + argc + !argc);
這將排除argv[0]
,但是即使argc == 0
(在Linux下可能也可能是其他操作系統),這種方式也很強大。
argv和argc是傳遞給main的參數。 在你的函數中你應該使用args [i]和args.length()
application::commandLine()
將args
作為參數,但它引用argc
和argv
,它們不在范圍內。 如果查看編譯器的實際錯誤消息,它應該包含一個文件名和行號,直接指向錯誤的位置。 在尋求錯誤消息的幫助時,請發布實際的錯誤消息而不是解釋它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.