main.cpp
#include<cstdio>//cstdio=stdio.h,stdio由std和io组成
using namespace std;
int a,b;
int main(){
freopen("test.txt","r",stdin);//freopen("filename","r",stdin); "r"只读,文件不存在会出错
freopen("output.txt","w",stdout);//freopen("filename","w",stdout); "w"只写,文件不存在将创建文件
//filename只写与main.cpp同目录下欲读写之文件名
//test.txt与main.cpp同目录
scanf("%d%d",&a,&b);
printf("运算结果是%d",a+b);
return 0;
}
test.txt
2 3
output.txt
运算结果是5
注:
如果直接修改test.txt中的数据保存后直接再次运行程序,则output.txt中的内容会被覆盖。
如果需要将结果输出到屏幕,将freopen(...stdout);删掉或注释掉即可。