C++ 关于 freopen 函数

时间:2021-03-17 14:47:39   收藏:0   阅读:0

C++ 关于 freopen 函数

名 称:freopen

所 属:stdio.h

功 能:用于重定向输入输出流。该函数可以在不改变代码原貌的情况下改变输入输出环境,但使用时应当保证流是可靠的。


实例

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int a,b,c;
	
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	
	cin >> a >> b;
	
	c = a + b;
	
	cout << c;
	
	return 0;
} 

对于以上代码,对控制台的读入和输出都无效,

它只会从文件 in.txt 中进行读入,从 out.txt 中输出。并且 in.txtout.txt 文件都需要与生成的 exe 文件在同一文件夹内

如下图a.1

技术图片



评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!