【问题标题】:sending output stream to char[] in c在c中将输出流发送到char []
【发布时间】:2012-08-09 15:50:57
【问题描述】:

我一直在使用dupfreopenstdout 重新路由到如下文件:

fflush(stdout);
fgetpos(stdout, &pos);
fd = dup(fileno(stdout));
freopen("stdout.out", "w", stdout);

我想做的是将它重新路由到char[],以便我可以操纵它。显然这在使用printf 编写时不是很有用,但是当使用写入stdout 的库时,在代码中获取输出会很有帮助,以便我可以在必要时对其进行操作。

【问题讨论】:

  • 为什么无法将文件读入内存?
  • @user315052 写入文件然后立即从该文件读取似乎是一个额外的步骤。输出不能直接发送到char[]吗?
  • 没有 API 可以将 stdout 更改为内存流。 GNU 的 libc 提供了fmemopenopen_memstream,但是它们会创建新的流,并且没有接口可以让一个流替换现有的流。
  • 这是您问题的镜像版本:stackoverflow.com/questions/1558772/…

标签: c stdout freopen


【解决方案1】:

不能保证分配给 stdout 有效,但也许它会在您的平台上有效,否则请参阅基于 shmem_open、mmap 和 fmem_open 的答案:https://stackoverflow.com/a/25327235/2332068

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2012-09-06
    • 2014-05-07
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多