Deprecated: Array and string offset access syntax with curly braces is deprecated in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/init.php on line 542
ja:cpp:shellcommand [GiriWiki]

ユーザ用ツール

サイト用ツール


ja:cpp:shellcommand

以前のリビジョンの文書です



Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/parser/handler.php on line 1458

Warning: Declaration of syntax_plugin_note::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/lib/plugins/note/syntax.php on line 79

Warning: Declaration of syntax_plugin_note::render($mode, &$renderer, $indata) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/lib/plugins/note/syntax.php on line 101

Warning: preg_match(): Compilation failed: invalid range in character class at offset 3120 in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/parser/lexer.php on line 118
A PCRE internal error occured. This might be caused by a faulty plugin

====== Shellコマンドを実行 ====== ---- ナイーブには * system 関数 <cstdlib> * popen 関数 <csdio> を使う2通りがある。\\ ただコマンドを実行したいだけであればsystem関数を使えば良いが、system関数の返り値はshellの終了ステータスであり、 実行結果の出力を得ることはできない。\\ コマンドの実行結果を得たい(例えば ls を実行してファイルのリストを得たい)時は popen関数 popen関数はFILEポインタを返り値に持ち、パイプ(コマンドの標準出力)を(仮想の)ファイルとして開く。 それを読み込むことでコマンドの実行結果を得ることができる。 <code cpp> char *command = "...";//実行したい任意のコマンド FILE *pipe; char buffer[256]; if( (pipe = popen(command,"r"))==NULL ){// パイプをopen std::cout << "failed." << std::endl; return 1; } while( fgets(buffer,sizeof(buffer),pipe)!=NULL ){ strtok(buffer,"\n\0");// bufferには改行コードが含まれてしまっているのでそれをNULLに置換 (#include <cstring>) .... .... // コマンドの標準出力を1行ずつ読んで(bufferに格納)好きな処理を実行 .... } pclose(pipe); // パイプをclose ※忘れずに! </code> 注意点として、[[http://panda.holy.jp/2014/07/312/|pcloseでストリームを閉じた後にプロセスの終了待ちをしている]]ため、 popenで呼び出したプロセスが終了しないままpcloseしてしまうと、そのプロセスが失敗してしまうらしい。

ja/cpp/shellcommand.1502967136.txt.gz · 最終更新: 2017/08/17 10:52 by kota