2017-03-26 16 views
-1

私は実際にargc、argv []を使ってファイルを読むことはしませんでしたが、プログラムの要件によって私はそれらを使うことができます。私の問題は、ヘッダーファイルのファイルを読み込み、mainの代わりに処理していることです。 main.cppの外側でargc、argv []入力を使用する方法はありますか?私のmain.cppファイルの外にargc、argvを渡すことはありますか?

"input.txt"などをargvに置き換えて使用したいヘッダーファイルです。

ではなく、output.txtと

void expression::convertToPostFix() 
{ 
    { 

    ofstream fout("output.txt"); 
    stack<char> stack; 
    stringstream postfix; 

    while (!obj.empty()) { 
    stack.push('('); 
    fout << "InFix is:\t"; 
    while (1) { 
     if ((obj.front() != ';') && (obj.front() != '.')) 
     { 
      const char current = obj.front();   
      cout << current; 
      fout << current; 

      obj.pop(); 
      if (isspace(current)) { 

      } 
      else if (isalnum(current)) { 
       postfix << current; 
      } 

      else if ('(' == current) { 
       stack.push(current); 
      } 

      else if (isOperator(current)) { 
       char rightOperator = current; 
       while (!stack.empty() && isOperator(stack.top()) &&        precedence(stack.top(), rightOperator)) { 
        postfix << ' ' << stack.top(); 
        stack.pop(); 
       } 
       postfix << ' '; 
       stack.push(rightOperator); 
      } 

      else if (')' == current) { 

       while (!stack.empty() && '(' != stack.top()) { 
        postfix << ' ' << stack.top(); 
        stack.pop(); 
       } 

       stack.pop(); 
       postfix << ' '; 
      } 

     } 
     else 
     { 
      obj.pop(); 
      break; 
     } 
    } 
    while (!stack.empty() && '(' != stack.top()) { 
     postfix << ' ' << stack.top(); 
     stack.pop(); 
    } 

    stack.pop(); 
    pfix = postfix.str(); 
    postfix.str(""); 
    cout << "\nPost fix is:\t" << pfix << endl << endl; 
    fout << "\nPost fix is:\t" << pfix << endl << endl; 
    } 

} 

}

のここで使いたい(それはここにも使用されたい)

expression::expression() 
{ 

ifix = ""; 
pfix = ""; 
last = 0; 

char chr; 
ifstream fin; 
fin.open("input.txt"); 


while (!fin.eof()) 
{ 
    fin >> chr; 

    if (!fin.eof()) 
    { 
     if (chr != ' ') 
     { 
      obj.push(chr); 
     } 
    } 


    } 

} 
+2

'argv [1]'を最初の引数を必要とする関数に渡しますか?もちろん、 'argc> = 2'を確認してください。 –

+1

ところで、一般的なコーディングガイドラインは、ヘッダファイルのソースファイルと宣言にコードを配置することです。 –

答えて

0

回答main関数のargcargv外に合格することが可能です。

非常に簡単な使用法プログラムで、コマンドライン引数を使用します。 argv [0]を引数としてshow_usageメソッドに渡します。

#include <iostream> 
#include <string> 
#include <vector> 

static void show_usage(std::string name) 
{ 
    std::cerr << "Usage: " << argv[0] << " <option(s)> SOURCES" 
       << "Options:\n" 
       << "\t-h,--help\t\tShow this help message\n" 
       << "\t-d,--destination DESTINATION\tSpecify the destination path" 
       << std::endl; 
} 

int main(int argc, char* argv[]) 
{ 
    if (argc < 3) { 
     show_usage(argv[0]); 
     return 1; 
    } 
    std::vector <std::string> sources; 
    std::string destination; 
    for (int i = 1; i < argc; ++i) { 
     std::string arg = argv[i]; 
     if ((arg == "-h") || (arg == "--help")) { 
      show_usage(argv[0]); 
      return 0; 
     } else if ((arg == "-d") || (arg == "--destination")) { 
      if (i + 1 < argc) { // Make sure we aren't at the end of argv! 
       destination = argv[i++]; // Increment 'i' so we don't get the argument as the next argv[i]. 
      } else { // Uh-oh, there was no argument to the destination option. 
        std::cerr << "--destination option requires one argument." << std::endl; 
       return 1; 
      } 
     } else { 
      sources.push_back(argv[i]); 
     } 
    } 
    return move(sources, destination); 
} 
1

はあなたの機能を拡張し、 argvargc

に渡します
expression::expression(int argc, char ** argv) 

コール:あなたの質問、YES

int main(int argc, char ** argv) 
{ 
    ... 
    expression::expression(argc, argv); 
    ... 
} 
+0

ありがとうございました! – Zephers

1

あなたmain関数が最初にすべきことは、std::vector<std::string>にそれらの文字配列を回すです:

std::vector<std::string> const args(argv, argv + argc); 

あなたはその後、std::vector<std::string>は、それが必要なのwhereeverすることを渡すことができます。

void expression::expression(std::vector<std::string> const &args) 
{ 
    // ... 
} 

// ... 

int main(int argc, char* argv[]) 
{ 
    std::vector<std::string> const args(argv, argv + argc); 
    expression::expression(args); 
} 

の場合ベクタが多くの中間層を通過しなければならないため、これは実行可能な解決策ではありません。より良いアプローチ。どこかに次の関数を作成します。main

std::vector<std::string>& CommandLineArgs() 
{ 
    static std::vector<std::string> args; 
    return args; 
} 

を、設定された値:それから読んで、あなたのプログラム内の他のどこでも

int main(int argc, char* argv[]) 
{ 
    std::vector<std::string> const args(argv, argv + argc); 
    CommandLineArgs() = args; 
} 

、:

void expression::expression() 
{ 
    auto const& args = CommandLineArgs(); 
    // ... 
} 

での作業をstd::vector<std::string>は、別の配列サイズの値を持つポインタを扱うよりはるかに簡単です。

関連する問題