OJ的文件流操作

我们刷题的时候除了编码外,测试也是非常重要的,当测试样例比较小的时候,我们完全可以手打,但是当测试样例比较大时候,我们就抓狂了~

相信不少人都知道利用文件流,但是应该还有新手跟我一样,一遍又一遍地输入测试样例~

其实很简单,就两句代码。

#include<cstdio> //包含头文件,c语言的就是stdio.h

freopen("in.txt","r",stdin);//在main 函数最开始加入,在当前工程下创建in.txt,之后加入数据

fclose(stdin);//在程序return 0前加入。


当然,如果纯粹是上面的代码,还是很琐碎,需要我们每次提交时候注释掉……

为了方便大家,很多OJ都使用ONLINE_JUDGE宏。

有就是我们这样就可以,在自己测试后,直接提交。

#include<iostream>
#include<cstdio>
using namespace std;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    

    #ifndef  ONLINE_JUDGE
    fclose(stdin);
    #endif
    
    return 0;

}

当然,如果你跟我一样是使用linux+codeblock来写代码的话,我这里还有一个shell脚本,可以自动生成一些每次都必写的东西。

其实我觉得用vim是很好的,但是哎,真的没有IDE,写代码很懊恼……

当然,我写shell是新手,所以大神不要见怪。。

当然,注释断,如果觉得没用就直接删掉吧。

这个是cpp的。

#!/bin/sh
#vi /usr/share/codeblocks/templates/wizard/console/cpp
Curtime=`date "+%Y-%m-%d "`
echo '/*******************************************************************************/  
/* OS           : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux 
 * Compiler     : g++ (GCC)  4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
 * Encoding     : UTF8 
 * Date         :' > /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp $Curtime >> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp
echo ' * All Rights Reserved XXX.
*****************************************************************************/
/* Description: *************************************************************** 
*****************************************************************************/
/* Analysis: ****************************************************************** 
*****************************************************************************/
/*****************************************************************************/  
' >> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp
echo '#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    

    #ifndef  ONLINE_JUDGE
    fclose(stdin);
    #endif
    
    return 0;

}

'>> /usr/share/codeblocks/templates/wizard/console/cpp/main.cpp

echo 'cpp.sh'
exit  

这个是C的。

#!/bin/sh
#vi /usr/share/codeblocks/templates/wizard/console/cpp
Curtime=`date "+%Y-%m-%d "`
echo '/*******************************************************************************/  
/* OS           : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux 
 * Compiler     : GCC  4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
 * Encoding     : UTF8 
 * Date         :' > /usr/share/codeblocks/templates/wizard/console/c/main.c $Curtime >> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo ' * All Rights Reserved by yaolong.
*****************************************************************************/
/* Description: *************************************************************** 
*****************************************************************************/
/* Analysis: ****************************************************************** 
*****************************************************************************/
/*****************************************************************************/  
' >> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo '

#include <stdio.h>  
#include <string.h>

int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    

    #ifndef  ONLINE_JUDGE
    fclose(stdin);
    #endif



     return 0;
}
'>> /usr/share/codeblocks/templates/wizard/console/c/main.c
echo 'c.sh'

exit  





原文地址:https://www.cnblogs.com/dengyaolong/p/3697216.html