首页 > Windows开发 > 详细

windows 控制台命令输出 捕获

时间:2020-07-07 18:19:38      阅读:82      评论:0      收藏:0      [点我收藏+]

#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
/* gcc defined unix */
#ifdef unix
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#define pipe(X) _pipe(X,40,O_BINARY)
#define fileno _fileno
#define dup2 _dup2
#define read _read

#endif
#include <assert.h>

int main()
{
     int fds[2];
     int res;
     char buf[256];
     int so;

    res = pipe(fds);
     assert(res == 0);

    so = fileno(stdout);
     //close stdout handle and make the writable part of fds the new stdout.
     res = dup2(fds[1], so);
     assert(res != -1);

    //printf("Hi theren3333333");
     system("dir");
     fflush(stdout);
     //reading should happen in a different thread

    res = read(fds[0], buf, sizeof(buf) - 1);
     assert(res >= 0 && res < sizeof(buf));
     buf[res] = 0;
     fprintf(stderr, "buf=>%s   --->n", buf);
     return 0;
}

windows 控制台命令输出 捕获

原文:https://www.cnblogs.com/thinkinc999/p/13261747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!