freopen

来源:互联网 发布:matlab编程教程 pdf 编辑:程序博客网 时间:2024/06/10 00:08

http://www.blog.edu.cn/user4/ericlee211/archives/2007/1782814.shtml

 

函数名:freopen
声明:FILE *freopen( const char *path, const char *mode, FILE *stream );
所在文件: stdio.h
参数说明:
path: 文件名。
mode: 文件打开的模式。和fopen中的模式(如r, w,)相同。
stream: 一个文件,通常使用标准流文件(stdin, stdout, stderr)。
返回值:成功,则返回一个path所指定的文件的指针。失败,返回NULL。(一般都不使用它的返回值)

功能:简单说,就是实现重定向。把预定义的几个标准流文件(stdin, stdout, stderr)定向到由path指定的文件中。
如下例:
int main()
{
//        freopen("debug//in.txt","r",stdin);
        while(cin>>ans)
        {
                          //to do .......
        }
        cout<<endl;
        return 0;
}
freopen("debug//in.txt","r",stdin)的作用就是把stdin重定向到debug//in.txt文件中,这样在用cin或是
用scanf输入时便不会从标准输入流提取数据。而是从in.txt文件中获取输入。只要把输入事先粘贴到
in.txt,调试时就方便多了。

原创粉丝点击