2017-03-26 13 views
-1

나는 파일을 읽는 데 argc, argv []를 사용하지는 않았지만 프로그램 요구 사항을 사용하기를 바랍니다. 내 문제는 헤더 파일의 파일을 읽고 대신 메인 파일을 처리하는 것입니다. main.cpp 외부에서 argc, argv [] 입력을 사용하는 방법이 있습니까?arg.c, argv를 내 main.cpp 파일 외부로 전달할 수 있습니까?

헤더 파일 "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

BTW, 일반적인 코딩 지침은 소스 파일과 헤더 파일의 선언에 코드를 배치하는 것입니다. –

답변

0

대답, YES 것이 가능은 argc 및 주요 기능의 argv 외부에 전달합니다.

매우 간단한 사용법 프로그램으로 명령 줄 인수를 사용합니다. 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을 (를)

에 전달하십시오. 6,
expression::expression(int argc, char ** argv) 

전화 : 귀하의 질문에

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>은 별도의 배열 크기 값을 가진 포인터를 처리하는 것보다 훨씬 쉽습니다.