1. 实现重定向:把预定义的标准流文件定向到由path指定的文件中。
标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;stderr是标准错误流,一般把屏幕设为默认。通过调用freopen,就可以修改标准流文件的默认值,实现重定向。
返回值: 成功,则返回一个path所指定文件的指针;失败,返回NULL。(一般可以不使用它的返回值)
因为freopen()函数重定向了标准流,使其指向前面指定的文件。
若要恢复句柄,可以重新打开标准控制台设备文件,只是这个设备文件的名字是与操作系统相关的。
4. 算法实例
图形应用控制台创建
This function is primarily used by graphics applications to create a console window. Graphics applications are initialized without a console. Console applications are normally initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).
API: BOOL AllocConsole(void);
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, and then it can call AllocConsole to create a new console or AttachConsole to attach to another console. If the calling process creates a child process, the child inherits the new console.
AllocConsole also sets up standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.