|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
[mw_shl_code=cpp,true]#include <stdio.h>
1 q. s8 q3 h# h5 y( o#include <stdlib.h>
, r8 V* g* b( I" f' b z#include <string.h>3 [: ^3 ~2 R: R3 Y& A. t
; _" o& U3 U0 P: i% J6 b0 V/ Q* k- |! {2 z
0 T) G. B& w, e8 N- W* m
int _System(const char * cmd, char *pRetMsg, int msg_len)
7 N, N r6 L& k7 H{2 v+ ]" c9 p8 g: H( k, ]
FILE * fp;
7 g7 a( f! S8 K2 h; p char * p = NULL;
- x# P- s9 n* E0 i/ r O int res = -1;
: g: W8 O9 R' U2 O! R! |9 P% F if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
5 e+ W+ j( v% v* Y, X {/ A1 Z3 {" L% S4 k7 G9 T4 K
printf("Param Error!\n");2 Y* z% Y' q: p2 D4 o# r
return -1;
' C3 `% x2 Y' x* ^: r }$ [3 F D( Y [
if ((fp = _popen(cmd, "r")) == NULL)
. Y# _/ P) ?& B" X {' p2 a f7 \* q! [- r6 L" A4 c& g
printf("Popen Error!\n");8 t3 ^( j6 j' G, B4 w
return -2;6 _5 \8 d* e3 s) P8 M1 }
}
/ z! V( E6 C: q' u; i4 } else
) w" V3 K; f/ |; _2 x3 [ {
s2 D/ K9 ~% E! u$ g2 Y memset(pRetMsg, 0, msg_len);, s3 ?6 r1 p- }8 W; A$ W
//get lastest result
! r0 E0 w2 j( a* R while (fgets(pRetMsg, msg_len, fp) != NULL)
- A, q3 V6 r+ f& h- s {
' @/ y6 i8 p8 Y: k7 p6 w printf("Msg:%s", pRetMsg); //print all info
2 `3 T( X0 {0 C; X: z. G }9 v8 a) Q8 m, B
% ]" ?' G' ^! R: e2 l9 C0 t
if ((res = _pclose(fp)) == -1)
% Y3 W/ q. o" D, o" E* D {6 o% u8 B; T& p: N1 `6 D1 a8 A1 r
printf("close popenerror!\n");
2 }9 o5 O% v# Z4 h M. a( W. o return -3;
8 e) x4 e4 b& u2 E; M }
+ j% a) r. A& s6 W5 @$ _% r0 Z pRetMsg[strlen(pRetMsg) - 1] = '\0';5 x/ Q" c/ t; V f. z! l* K
return 0;
* m! U8 T- n+ a- G0 v, J" ~+ q" M }
' ~4 Y% F6 d" U1 b8 V+ X; _ M}
& I1 h1 x9 x) Z4 b* a T$ G1 y5 n 0 z8 G% N2 ^1 l. p* l+ X( l
int main()
8 Y+ G" k* _* X6 v5 Y* ?2 H3 j3 E0 |{
2 _, r3 A& k, C+ S8 v //test cmd
7 X2 d0 T: ~' N, h% V% K- h9 b char *cmd = "python d:\\PythonProjects\\Demo1.py [1]";3 L% Z3 l m+ W( |
char a8Result[128] = { 0 };
3 F+ @1 p7 f3 F! P int ret = 0;3 P) m- p/ M6 a# I# W0 ?! b
ret = _System(cmd, a8Result, sizeof(a8Result));
8 u! j5 F+ F3 U printf("ret = %d \na8Result = %s\nlength = %d \n", ret, a8Result, strlen(a8Result));
) H7 Z6 l' m# g! Y, c geTChar();
1 j0 v/ Q }+ y7 w return 0;8 e( o$ z& H' ?; D8 k# m( h5 P
}4 i9 f T& N7 ^# g3 _: f f1 n9 ?
[/mw_shl_code]
1 L! t9 O$ O6 Z5 f |
|