Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283072
  • 博文数量: 33
  • 博客积分: 861
  • 博客等级: 军士长
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-26 09:35
文章存档

2013年(1)

2012年(8)

2011年(25)

分类:

2012-05-10 10:52:57

原文地址:C++文件操作 作者:mengl_08

源代码:

#include
#include
#include
struct worker
{
char name[30];
char department[30];
float basepay,allowance,total;
};
void main()
{
char filename[30]="paydata.txt";
struct worker data;
ofstream outstuf;
outstuf.open(filename,ios::out|ios::binary);
if(!outstuf) { cout<<"打开文件失败!"<
outstuf<<"这是一个和工人工资有关的文件.\n"<
char ch='N';
while(ch!='y'&&ch!='Y')
    {
cout<<"请输入工人名字,工人住址及其基本工资和奖金:"<
cin>>data.name>>data.department>>data.basepay>>data.allowance;//插入数据
outstuf<
cout<<"是否结束输入?Y or N?"<
cin>>ch;
}
outstuf.close();
//以下为改动操作*********
ifstream instuf;  
instuf.open(filename,ios::in|ios::binary);
if(!instuf)  {  cout<<"文件打开失败,程序退出!"<
instuf.clear();
int i=0;
while(!instuf.eof())
{
instuf>>data.name>>data.department>>data.basepay>>data.allowance>>data.total;
data.basepay=data.basepay+100;;
data.total=data.total+100;
cout<<"现在输出工人的名字、住址、基本工资、奖金、总共资金:"<
cout<
i++;
}
instuf.close();
}

阅读(2205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~