freopen及其拓展函数,大数据调试so easy (ง •_•)ง

刚开始大工程,体验到大数据调试的难度,困顿且无奈的时候想到了曾经遇到大佬使用的freopen函数,

当时不明觉厉,什么都没想就直接收藏起来了,缘分啊 d=====( ̄▽ ̄*)b ,太妙了

我用萌新的角度理解了这个函数,下文贴代码解释,

看不明白可以先创建txt文档,命名为in,然后写 两个 用一个空格间隔的 数字 进去,保存,

然后直接运行代码,看多出来的txt文档,就知道发生了什么了。

很简单很好用 d=====( ̄▽ ̄*)b

/*
freopen("in.txt","r",stdin)的作用就是把标准输入流stdin重定向到in.txt文件中,
这样在用scanf或是用cin输入时便不会从标准输入流读取数据,
而是从in.txt文件中获取输入。
只要把输入数据事先粘贴到in.txt,调试时就方便多了。 
类似的,
freopen("out.txt","w",stdout)的作用就是把stdout重定向到out.txt文件中,
这样输出结果需要打开out.txt文件查看。

简言:
freopen("in.txt","r",stdin);   //输入重定向,输入数据将从in.txt 文件中读取
freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 


需要说明的是: 
1. 在调用freopen函数实现重定向时,路径名字一定要写正确。 
2. 可以不使用输入重定向,仍然通过键盘输入,也可以不使用输出重定向,仍然在控制台查看输出,这都是可以的,看自己需求来定。

*/ 
#include <cstdio> 
int main() 
{
/*
若是在做OJ题目  按以下格式更方便 
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
*/
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    //freopen("out.txt","aw",stdout);// 保留原文件内容 
    int a,b;
    while(~scanf("%d %d",&a,&b))
    printf(" %4d + %4d == %4d\n",a,b,a+b);
    
    puts("???");//out.txt文本只有“???”没有“完成” 
     
    fclose(stdin);//关闭文件 

    fclose(stdout);//关闭文件
    
    freopen("CON","w",stdout);//返回原输出屏幕 
    
    puts("完成");
    //同理,返回原输入,注意路径必须相同
    /* 
    freopen("CON","r",stdin);
    puts("输入a的值"); 
    scanf("%d",&a);
    printf("%d\n",a); 
    */
    return 0;
} 
View Code

谢谢学习源。

博客的简介:
https://blog.csdn.net/xylon_/article/details/81257268

官方解释:(推荐QQ浏览器,自带翻译)
http://www.cplusplus.com/reference/clibrary/cstdio/freopen.html

拓展学习:
https://www.cnblogs.com/yym2013/p/3236226.html

posted @ 2020-06-13 17:45  Renhr  阅读(140)  评论(0编辑  收藏  举报