|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
[mw_shl_code=cpp,true]#include <stdio.h>$ |3 j2 A, j" q2 y- M
#include <stdlib.h>
; X1 q. x5 m' P' t8 i6 g6 `( R#include <string.h>. J1 W5 d% |1 P+ L4 b2 w* n
$ W8 L7 z/ Q" p" ~
, b2 F- A7 Y/ G2 p; N. c. J) e
int _System(const char * cmd, char *pRetMsg, int msg_len)
/ b" M2 H( x- c% X# ~) f% b/ O{
8 d6 Z( ~+ s( e( k5 c9 v8 ]5 A' g FILE * fp;" P u- w1 g% b# z# P$ o
char * p = NULL;7 W A* F6 f' ^2 P* o0 _
int res = -1;% E8 V; H; {) P7 Y- ~
if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
, \% L: Z8 V) g+ Q {1 J6 p! F. ?1 L; q
printf("Param Error!\n");( p9 W* x# a3 \2 X: L! a
return -1;
' q# V) t {1 e O }
2 q1 C6 u/ R& D. p! T4 w. L if ((fp = _popen(cmd, "r")) == NULL)* s. Q* g" H( z& m
{# i; ]2 O/ ^- f. J u/ x# t
printf("Popen Error!\n");
0 W! |2 F2 ^" v3 e8 J return -2;* g& Q6 P- ~: f
}1 x8 S' L6 ~) f) L
else8 J( Y# {5 X6 K5 y7 h' A0 n3 s4 `4 f
{
( `6 j; p- t& D) I( ^ memset(pRetMsg, 0, msg_len);
9 @9 ~' Y( @* U //get lastest result 5 }) r3 [6 P6 r* o" B
while (fgets(pRetMsg, msg_len, fp) != NULL)# r j6 f2 Q' q6 ^3 K: a
{
- L c2 [6 ^3 I6 |2 g% V- U# ~ printf("Msg:%s", pRetMsg); //print all info
" p |. }8 U( q7 ]. W' W5 v }: I* Y% q! g5 I8 M, V" ]
: a1 }4 m* N7 v( ]+ T
if ((res = _pclose(fp)) == -1)
4 T/ L& q% y- W. g3 u. D* v {0 }. O" u( H; z# L
printf("close popenerror!\n");$ c: [3 w, f0 G7 J9 ?7 W
return -3;. r9 X) A k. A" A0 i t# s$ N
}
& y* M; d% e" N; T+ A% V pRetMsg[strlen(pRetMsg) - 1] = '\0';
& n# K- u- R. ^: Y) E/ z' D return 0;
6 P: Q4 |+ i9 O: y* a9 u }
8 Y2 u) B W2 p' N. ]+ ^}
, A2 x* ]7 [* F2 P3 n* z
. S; @( D5 c" E& t$ w( I2 Oint main(); b- }. ~- i9 w) |% t/ l! ]. C
{
9 S) |# c4 T* Q7 Q, m" _ //test cmd 5 E1 E, ~9 y6 l
char *cmd = "python d:\\PythonProjects\\Demo1.py [1]";
" o* {: t# O7 a( e3 ~* t9 h char a8Result[128] = { 0 };2 U; G$ y! R3 z
int ret = 0;1 i, I9 }7 E# w6 l
ret = _System(cmd, a8Result, sizeof(a8Result));1 k0 Q1 \% b1 @9 E6 q: S' q
printf("ret = %d \na8Result = %s\nlength = %d \n", ret, a8Result, strlen(a8Result));. K0 b) N- I; P# l
geTChar();3 |) C. R ]- \( _" A) q [
return 0;9 S. d& H+ [' ?$ a: [
}/ Z# ^% R. f5 _5 a0 V
[/mw_shl_code]! \- i+ Y6 g- e& [! s6 r
|
|