使用freopen后如何在stdout中写入 [英] How to write in stdout after using freopen

查看:318
本文介绍了使用freopen后如何在stdout中写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

freopen -ing stdout之后,如何在终端上打印?

After freopen-ing stdout, How can I print on terminal?

freopen("out", "w", stdout); // reopen stdout
/* something */
printf("Now I want to print this on terminal");

推荐答案

我相信这是您要寻找的:

I believe this is what you are looking for:

一旦我使用过freopen,如何获得原始的stdout(或stdin)?

没有便携式解决方案.但是该链接还说明了使用您自己的流和不可移植的可能解决方案.在大多数posix系统上都可以使用的解决方案.

There's no portable solution. But the link also explains a possible solution using your own stream and a non-portable solution that'll work on most posix systems.

没有一个好方法.如果您需要切换回去,最好的解决方案 首先不要使用freopen.尝试使用自己的 显式输出(或输入)流变量,可以在以下位置重新分配 ,同时保持原始标准输出(或标准输入)不受干扰.为了 例如,声明一个全局

There isn't a good way. If you need to switch back, the best solution is not to have used freopen in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leaving the original stdout (or stdin) undisturbed. For example, declare a global

FILE *ofp;

并将所有对printf(...)的调用替换为fprintf(ofp,...). (显然,您还必须检查对putchar和puts的调用.) 然后,您可以将ofp设置为stdout或其他任何设置.

and replace all calls to printf( ... ) with fprintf(ofp, ... ). (Obviously, you'll have to check for calls to putchar and puts, too.) Then you can set ofp to stdout or to anything else.

您可能想知道是否可以完全跳过freopen并执行某些操作 喜欢

You might wonder if you could skip freopen entirely, and do something like

FILE *savestdout = stdout;
stdout = fopen(file, "w");    /* WRONG */

让自己以后可以恢复标准输出

leaving yourself able to restore stdout later by doing

stdout = savestdout;      /* WRONG */

,但是这样的代码不太可能起作用,因为stdout(和stdin 和stderr)通常是无法重新分配的常量(其中 这就是为什么freopen首先存在的原因.

but code like this is not likely to work, because stdout (and stdin and stderr) are typically constants which cannot be reassigned (which is why freopen exists in the first place).

有可能以一种非便携式的方式保存信息 关于在调用freopen来打开某个文件之前的流的信息, 以便以后可以恢复原始流.最多 直接可靠的方法是处理基础文件 使用特定于系统的调用(例如dup或dup2)的描述符,如果 可用的.另一种是复制或检查FILE的内容 结构,但这是极其不便和不可靠的.

It may be possible, in a nonportable way, to save away information about a stream before calling freopen to open some file in its place, such that the original stream can later be restored. The most straightforward and reliable way is to manipulate the underlying file descriptors using a system-specific call such as dup or dup2, if available. Another is to copy or inspect the contents of the FILE structure, but this is exceedingly nonportable and unreliable.

在某些系统上,您也许可以重新打开特殊的设备文件 (例如Unix的现代版本下的/dev/fd/1)仍然 附加到(例如)原始标准输出.你可以,在 在某些系统中,显式重新打开控制终端,但这 不一定是您想要的,因为原始的输入或输出 (即在您调用freopen之前,stdin或stdout是什么) 已从命令行重定向.

Under some systems, you might be able to reopen a special device file (such as /dev/fd/1 under modern versions of Unix) which is still attached to (for example) the original standard output. You can, under some systems, explicitly re-open the controlling terminal, but this isn't necessarily what you want, since the original input or output (i.e. what stdin or stdout had been before you called freopen) could have been redirected from the command line.

这篇关于使用freopen后如何在stdout中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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