传递命令行参数

传递命令行参数

问题描述:

好吧我已经更新了我的代码,但我仍然不完全确定如何使用我传递的命令行参数的向量。我试图设置它像我下面的代码,但它不会编译。它给了我找不到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(); 
    } 
}; 

我只是写:

vector<string> args(argv + 1, argv + argc + !argc); 

这样就可以排除argv[0],但在某种程度上这是稳健的,即使argc == 0(可能在Linux下,和也许其他操作系统也是如此)。

+0

是你如何在传递参数之前将原始矢量在main中对齐? – shinjuo 2010-03-02 05:48:40

+0

@shinjuo:构建一个向量'args',并用所有命令行参数初始化为一次。 (我很抱歉,我不知道如何阅读你的评论。) – 2010-03-02 05:51:56

+0

只是为了确保我正确理解你,我可以删除所有主要的推回,只是使用它? – shinjuo 2010-03-02 05:53:22

application::commandLine()需要args作为参数,但它是指argcargv,它们不在范围内。如果您查看编译器的实际错误消息,它应该包含一个文件名和行号,直接指向错误的位置。当要求提供有关错误消息的帮助时,请发布实际的错误消息而不是解释它。

+0

我现在发布了实际的错误消息 – shinjuo 2010-03-02 06:18:36

argv和argc是传递给main的参数。在你的函数中你应该使用args [i]和args.length()

+0

我不知道我知道你在说什么在使用这些? – shinjuo 2010-03-02 06:19:26

+1

在您的应用程序方法中,使用args向量而不是argc和argv变量。 – DougS 2010-03-02 08:06:35