2

I'm writing a c++ program and I want people to be able to operate it from the terminal. The only thing I know how to do is cin which, though upon receiving the program could act, I wouldn't call a command. Thanks!!

4

2 に答える 2

3

試す

#include <iostream>
int main(int argc, char* argv[])
{
    std::cout << "Command: " << argv[0] << "\n";
    for(int loop = 1;loop < argc; ++loop)
    {
        std::cout << "Arg: " << loop << ": " << argv[loop] << "\n";
    }
}
于 2012-12-25T03:27:36.833 に答える
0

プログラムでは、コマンド ライン引数を受け入れる代替int mainシグネチャを使用します。

int main(int argc, char* argv[]);
// argc = number of command line arguments passed in
// argv = array of strings containing the command line arguments
// Note: the executable name is argv[0], and is also "counted" towards the argc count

また、実行可能ファイルの場所をオペレーティング システムの検索パスに入れることをお勧めします。これにより、フル パスを入力しなくても、どこからでも呼び出すことができます。たとえば、実行可能ファイルの名前がfooで、/home/me(Linux の場合) にある場合は、次のコマンド (ksh/bash シェル) を使用します。

export PATH=$PATH:/home/me`

Windows では、パスを環境変数に追加する必要があります%PATH%

foo次に、通常どおり、どこからでもプログラムを呼び出します。

foo bar qux
(`bar` and `qux` are the command line arguments for foo)
于 2012-12-25T03:32:28.380 に答える