函数名: freopen
功 能: 替换一个流,或者说重新分配
文件指针,实现重定向。如果stream流已经打开,则先关闭该流。如果该流已经定向,则freopen将会清除该定向。此函数一般用于将一个指定的文件打开一个预定义的流:标准输入、标准输出或者标准出错。
用 法: FILE *freopen(const char *filename,const char *type, FILE *stream);
返回值:如果成功则返回该指向该stream的指针,否则为NULL。
举例1:
int main()
{
/* redirect standard output to a file */
if (freopen("D:\\OUTPUT.txt", "w", stdout)==NULL)
fprintf(stderr, "error redirecting stdout\n");
/* this output will go to a file */
printf("This will go into a file.");
/* close the standard output stream */
fclose(stdout);
return 0;
}
举例2:
如果上面的例子您没看懂这个函数的用法的话,请看这个例子。这个例子实现了从stdout到一个文本文件的重定向。即,把输出到屏幕的
文本输出到一个文本文件中。
#include <stdio.h>
int main()
{
int i;
if (freopen("D:\\OUTPUT.txt", "w", stdout)==NULL)
fprintf(stderr, "error redirecting\stdout\n");
for(i=0;i<10;i++)
printf("%3d",i);
printf("\n");
fclose(stdout);
return 0;
}
在VC++6.0中运行一下,你会发现,十个数输出到了D盘根目录下文本文件OUTPUT.txt中。
举例3:
从文件in.txt中读入数据,计算加和输出到out.txt中
#include <iostream.h>
#include <cstdio.h>
using namespace std;
int main()
{
freopen("in.txt","r",stdin); /*如果in.txt不在连接后的exe的目录,需要指定路径如D:\\in.txt*/
freopen("out.txt","w",stdout);/*同上*/
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
printf("%d\n",a+b);
fclose(stdin);
fclose(stdout);
return 0;
}
若要返回到显示默认 stdout) 的 stdout,使用下面的调用:
freopen( "CON", "w", stdout ); //输出到控制台"CON"
检查 freopen() 以确保重定向实际发生的返回值。
下面是短程序演示了 stdout 时重定向:
运行代码
// Compile options needed: none
#include <stdio.h>
void main(void)
{
FILE *stream ; //将内容写到file.txt, "W"是写 ("r"是读)
if((stream = freopen("file.txt", "w", stdout)) == NULL)
exit(-1); printf("this is stdout output\n");
stream = freopen("CON", "w", stdout);stdout 是向程序的末尾的控制台重定向
printf("And now back to the console once again\n");
}
http://baike.baidu.com/view/656692.htm
Allocates a new console for the calling process.
Syntax
BOOL WINAPI AllocConsole(void);
Parameters
This function has no parameters.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
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, then it can callAllocConsole 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 initializes 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.
This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console. Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).
Requirements
Minimum supported client | Windows 2000 Professional [desktop apps only] |
---|
Minimum supported server | Windows 2000 Server [desktop apps only] |
---|
Header |
Wincon.h (include Windows.h)
|
---|
Library |
Kernel32.lib
|
---|
DLL |
Kernel32.dll
|
---|
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx