且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重定向输出从标准输出为一个字符串?

更新时间:2023-02-04 23:40:25

 字符字符串[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