freopen()函数详解

freopen函数详解及在OJ评测系统中的使用

在这里插入图片描述
若要打开一个二进制文件,需要加上一个“b”字符。

重定向打开

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);

重定向恢复

  • win
    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    
  • linux
    freopen("/dev/console", "r", stdin)
     freopen("/dev/console", "w", stdout);
    

在OJ系统中,常见的用法:

在本地编译时

#define DONLINE_JUDGE
/**
 *
 */
#ifdef DONLINE_JUDGE
	freopen("stdin.txt","r",stdin);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);

或者在编译时加上-DONLINE_JUDGE
或者使用Cmake的可以在CMakeLists.txt中加入add_compile_definitions(DONLINE_JUDGE)

猜你喜欢

转载自blog.csdn.net/m0_47917394/article/details/124200112
今日推荐