freopen ,AllocConsole

函数名: freopen
功 能: 替换一个流,或者说重新分配 文件指针,实现重定向。如果stream流已经打开,则先关闭该流。如果该流已经定向,则freopen将会清除该定向。此函数一般用于将一个指定的文件打开一个预定义的流:标准输入、标准输出或者标准出错。
用 法: FILE *freopen(const char *filename,const char *type, FILE *stream);
头文件: stdio.h
返回值:如果成功则返回该指向该stream的指针,否则为NULL。
举例1:
#include < stdio.h>
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;
}
举例4: [1]
若要返回到显示默认 stdout) 的 stdout,使用下面的调用:
freopen( "CON", "w", stdout ); //输出到控制台"CON"
检查 freopen() 以确保重定向实际发生的返回值。
下面是短程序演示了 stdout 时重定向:
运行代码
// Compile options needed: none
#include <stdio.h>
#include < stdlib.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

C++
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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
c语言中的freopen函数用于实现重定向,将预定义的标准流文件定向到指定的文件中。具体来说,它可以将stdin(标准输入流,默认为键盘)、stdout(标准输出流,默认为屏幕)和stderr(标准错误流,默认为屏幕)重定向到指定的文件。 使用freopen函数需要包含头文件iostream和stdio.h,并使用命名空间std。函数的原型如下: ```c FILE* freopen(const char* path, const char* mode, FILE* stream); ``` 其中,path是重定向文件的路径,mode是打开文件的模式(如“r”表示读取,"w"表示写入,"a"表示追加),stream是要重定向的流文件。 举个例子,假设我们使用以下代码: ```c freopen("D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest.txt","w",stdout); freopen("D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest1.txt","r",stdin); ``` 第一行代码的作用是将stdout重定向到"D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest.txt"文件中,这样输出结果就可以通过打开freopenTest.txt文件来查看。 第二行代码的作用是将stdin重定向到"D:\\CZHSoftware\\C语言\\Dev编程h\\freopen\\freopenTest1.txt"文件中,这样在使用scanf或cin输入时,会从freopenTest1.txt文件中获取输入。 以下是回答您的问题后的几个相关问题: 相关问题: 1. 你能解释一下重定向的作用和用途吗? 2. 除了freopen,c语言中还有其他方法可以实现重定向吗? 3. 重定向会对程序的执行效率有影响吗?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值