acm程序调试时的输入

来源:互联网 发布:淘宝怎么申请质量鉴定 编辑:程序博客网 时间:2024/06/04 19:22

每次做ACM题的时候要输入题目中的数据来测试,但是往往得调试很多次,特别是当有大量的数据要输入时,

而freopen函数就提供了一种简单的解决方法

函数名: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,调试时就方便多了。
                                                          ------------------------by 王海斌