重定向输出从标准输出为一个字符串? [英] redirect output from stdout to a string?

查看:512
本文介绍了重定向输出从标准输出为一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C
我想从重定向一个标准输出过程的输出,写入了共享内存段,它可以被看作是一个字符数组或字符串的指针结果
我知道有dup2但它需要的文件discriptors作为参数不是一个指向数组的指针。有没有办法把它重定向到一个字符串?


解决方案

 字符字符串[SIZE]
freopen函数(的/ dev / null的,一,标准输出);
则setbuf(标准输出,字符串);

请参见 freopen函数并的则setbuf 对它们的定义

in C i want to redirect the output of a process from stdout to write to a "shared memory segment" which can be thought of as a char array or a string with a pointer
i know that there is dup2 but it takes file discriptors as argument not a pointer to an array. is there any way to redirect it to a string?

解决方案

char string[SIZE];
freopen("/dev/null", "a", stdout);
setbuf(stdout, string);

see freopen and setbuf for their definitions

这篇关于重定向输出从标准输出为一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆