|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
[mw_shl_code=cpp,true]#include <stdio.h>. i! @, Q& s/ _/ j2 o( g1 M
#include <stdlib.h>
/ I x2 S) M8 I* }8 Q#include <string.h>
B6 [$ W z Z' `9 a
/ L7 [$ t9 c# Y# i 9 `8 a) x% _: Y5 U2 H3 W
int _System(const char * cmd, char *pRetMsg, int msg_len)5 [( O6 p% e% C6 ~4 Q
{1 A2 }2 w) Y) r" B3 j% j4 o1 P
FILE * fp;
7 S+ H9 x5 B; ~9 f, e: W char * p = NULL;8 F/ Z' `" w* F' x8 H* I# I B
int res = -1;
, K3 I3 _3 _1 ]; e+ ^ if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
6 ?# D" B! U0 v3 J {
& K& u# G. t* v: `4 _2 s printf("Param Error!\n");; ^! h1 A% D6 N. X9 Z/ O
return -1;
a% G! E1 l7 l$ C" H# \8 \) I6 \3 e }; h9 f+ b; }* ^5 ^7 R% Y1 i
if ((fp = _popen(cmd, "r")) == NULL)
. ]9 T3 b$ C7 w2 r {
2 r; W$ ~# P7 q7 z; n- G printf("Popen Error!\n");, n- U1 Y2 w) c$ U1 l9 u
return -2;5 ^. B& d1 \( G; z4 Y" ^* s) L% U
}$ J- T" \3 [/ `
else* w5 }, e+ A G N2 w5 F$ `
{( L. L% y4 T, `! ]
memset(pRetMsg, 0, msg_len);& g1 Q+ _- H. g
//get lastest result
4 {) b* q8 u3 z while (fgets(pRetMsg, msg_len, fp) != NULL)) i) ^$ D7 z. b
{ ?- a6 d3 E; @8 ]( p! L% {
printf("Msg:%s", pRetMsg); //print all info 0 |& `% f! j7 z! |( [, R
}
" z& Y3 Y$ N! p8 P* `: v. @
: @9 X! D, u) W+ P4 E if ((res = _pclose(fp)) == -1)
+ \* a; ^$ w ~& V7 w, T( ? {
4 E- E: k0 I( k& d7 C+ s9 O& g printf("close popenerror!\n");
) r, g& ]6 h2 u, U- O0 R+ u return -3;
; J; B) I/ f8 R5 H1 d }5 ~, P; @6 w: h6 Y- q2 T
pRetMsg[strlen(pRetMsg) - 1] = '\0';
5 y' Z- O3 R. b return 0;" x( r5 S5 s+ D; ~3 x, Y
}* Y( S% O) U+ w. } {
}9 q. t: e2 I5 t3 R2 k3 L
8 I, }- }, t) d/ e% H4 yint main()2 J2 o; \7 N( M! @
{
" [* O9 [, o; G/ Q //test cmd
( o: V) {: l- h$ | char *cmd = "python d:\\PythonProjects\\Demo1.py [1]";
4 i5 O7 ~$ h- o- Y2 s% x8 G. I4 v char a8Result[128] = { 0 };2 ?* a8 [ R' h2 V. W- ^' @
int ret = 0;
- C3 g6 P% U& y4 |9 g2 e2 C' } ret = _System(cmd, a8Result, sizeof(a8Result));$ N. p" x6 c) N! S
printf("ret = %d \na8Result = %s\nlength = %d \n", ret, a8Result, strlen(a8Result));
3 b0 e# H9 [0 c- y" r2 l- w) H: _2 n geTChar();+ x+ z1 j0 H/ }; J: a5 c4 n
return 0;
8 g% @" z! E3 q4 E7 S}5 I. }4 {, K8 r: l: n; |
[/mw_shl_code]! I/ h! z( D- R; [8 j( j
|
|