管道小实例

_popen函数:建立管道IO。

定义:

FILE * popen ( const char * command , const char * type );

_pclose函数:关闭管道。

 

例子,将输入放进cmd.exe中,并获取结果。

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<Windows.h>
 6 
 7 int exeshell(char *cmd)
 8 {
 9     FILE *fp = _popen(cmd, "r");    //创建管道
10     if (fp == NULL) {
11         printf("打开管道失败\n");
12         return 0;
13     }
14     else {
15         while(!feof(fp)) {
16             char str[256] = { 0 };
17             if (fgets(str, 256, fp)) {
18                 printf("%s",str);
19             }
20         }
21         return 0;
22     }
23     _pclose(fp);    //关闭管道
24 }
25 
26 int main() {
27     char s[256] = { 0 };
28     while (scanf("%s", &s) != EOF)
29         exeshell(s);
30     system("pause>nul");
31     return 0;
32 }

 

转载于:https://www.cnblogs.com/HadesBlog/p/7668235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值