首页 > 编程语言 > 详细

C++ 文件读写示例

时间:2014-02-23 18:17:51      阅读:490      评论:0      收藏:0      [点我收藏+]

输入文件被看作输入流

输出文件被看作输出流

进行文件操作需包含头文件fstream

//this program repeatedly reads an income from
//the file incom.in until end-of file.
//Income under 6000 greenbacks is taxed at 30 percent,
//and income greater than or equal to 6000
//greenbacks is taxed at 60 percent.
//After reading each income, the program prints the 
//income and tax.

#include<fstream>

using namespace std;

const int cutoff=6000;
const float rate1=0.3;
const float rate2=0.6;

int main(){
	ifstream infile;
	ofstream outfile;
	int income, tax;
	infile.open("income.in");
	outfile.open("tax.out");
	while(infile>>income){
		if(income < cutoff)
			tax = rate1 * income;
		else
			tax = rate2 * income;

		outfile<<"tax= "<<tax<<endl;
	}

	infile.close();
	outfile.close();
	return 0;
}


C++ 文件读写示例

原文:http://blog.csdn.net/slience_perseverance/article/details/19722289

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!