好吧我已經更新了我的代碼,但我仍然不完全確定如何使用我傳遞的命令行參數的向量。我試圖設置它像我下面的代碼,但它不會編譯。它給了我找不到argc和argv的錯誤:錯誤C2065:'argc ':未聲明的標識符 1> C:\用戶\克里斯\文件\視覺工作室2008 \項目\ 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();
}
};
原帖
我想通過命令行參數出主。這是一個高級C++類的作業。我需要傳遞一個向量的命令行,我不知道我是否正確地做了一切。我會像我一樣將它傳遞給一個矢量嗎?還有一個copy()命令可以用來將命令行參數複製到一個向量中而不是推回?
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();
}
};
是你如何在傳遞參數之前將原始矢量在main中對齊? – shinjuo 2010-03-02 05:48:40
@shinjuo:構建一個向量'args',並用所有命令行參數初始化爲一次。 (我很抱歉,我不知道如何閱讀你的評論。) – 2010-03-02 05:51:56
只是爲了確保我正確理解你,我可以刪除所有主要的推回,只是使用它? – shinjuo 2010-03-02 05:53:22