C++ プログラムからの引数を使用してコマンド ライン プログラムを実行するにはどうすればよいですか? これは私がオンラインで見つけたものです:
http://www.cplusplus.com/forum/general/15794/
std::stringstream stream;
stream <<"program.exe "<<cusip;
system(stream.str().c_str());
しかし、実際のプログラムの場所を受け入れないように見えるので、これを適用する方法がわかりません。私の希望は、次のようなものになることでした。
std::stringstream stream;
stream <<"C:\Tests\SO Question\bin\Release\HelloWorld.exe "<<"myargument";
system(stream.str().c_str());
これにより、バックスラッシュに関連するいくつかの警告が表示され、プログラムは機能しません。特定の場所にプログラムがあることを期待していますか?
これは、コンソールに表示される出力です。
「C:\Tests」は、内部コマンドまたは外部コマンド、操作可能なプログラムまたはバッチ ファイルとして認識されません。
補遺:
したがって、ジョンの答えに基づいて、私にとって正しいバージョンは次のようになります。
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstring>
int main(int argc, char *argv[])
{
std::stringstream stream;
stream << "\"C:\\Tests\\SO Question\\bin\\Release\\HelloWorld.exe\""
<< " " // don't forget a space between the path and the arguments
<< "myargument";
system(stream.str().c_str());
return 0;
}