以前用过文件读写,但是由于平时不怎么需要这个,所以久而久之也就忘了,直到今天做了一道案例数据非常水,需要自己造特么多测试数据的题,尤其这个题是输入的是maze,所以,没办法,重新学习一下文件读写,以备后用。
#include<cstdio> //freopen在这个头文件中
#include<iostream>
using namespace std;
int main()
{
int a,b;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout); //若想从控制台输出,就删掉这句和下面的fclose(stdout);
while(cin>>a>>b){
cout<< a+b <<endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
in.txt需要和.cpp放在同一个文件夹中
测试:
1:在.cpp文件目录下新建文本文档取名“in”,我的电脑上不加.txt
2:在in.txt中添加数据
3:运行程序
弹出的窗口并没有输出什么
源文件夹中出现out.txt