求解释C++的read函数??
时间:2011-11-29
来源:互联网
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int num=10;
struct people
{
char name[num];
int age;
float pay;
};
int main()
{
people pp={"陈涛",25,945.34f};
ofstream fout("people.txt",ios::out|ios::binary);
fout.write((char*)&pp,sizeof pp);
fout.close();
people pp1={"李力持",45,1065.58f};
fout.open("people.txt",ios::in|ios::out|ios::binary);
fout.seekp(sizeof pp,ios::beg);
fout.write((char*)&pp1,sizeof pp1);
fout.close();
people pp2={"刘伟",40,1060.58f};
fout.open("people.txt",ios::in|ios::out|ios::binary);
int a=(sizeof pp)+(sizeof pp1);
fout.seekp(a,ios::beg);
fout.write((char*)&pp2,sizeof pp2);
fout.close();
fstream InOut;
InOut.open("people.txt",ios::in|ios::out|ios::binary);
int count=0;
fout.seekp(a,ios::beg);
while (InOut.read((char*)&pp,sizeof pp))
{
cout<<count++<<":"<<setw(num)<<pp.name<<":"
<<setprecision(0)<<setw(12)<<pp.age
<<setprecision(2)<<setw(16)<<pp.pay<<endl;
}
InOut.close();
cout<<"完成\n";
return 0;
}
其中的InOut调用read函数为什么可以把PP,PP1,PP2读出来,
#include <fstream>
#include <iomanip>
using namespace std;
const int num=10;
struct people
{
char name[num];
int age;
float pay;
};
int main()
{
people pp={"陈涛",25,945.34f};
ofstream fout("people.txt",ios::out|ios::binary);
fout.write((char*)&pp,sizeof pp);
fout.close();
people pp1={"李力持",45,1065.58f};
fout.open("people.txt",ios::in|ios::out|ios::binary);
fout.seekp(sizeof pp,ios::beg);
fout.write((char*)&pp1,sizeof pp1);
fout.close();
people pp2={"刘伟",40,1060.58f};
fout.open("people.txt",ios::in|ios::out|ios::binary);
int a=(sizeof pp)+(sizeof pp1);
fout.seekp(a,ios::beg);
fout.write((char*)&pp2,sizeof pp2);
fout.close();
fstream InOut;
InOut.open("people.txt",ios::in|ios::out|ios::binary);
int count=0;
fout.seekp(a,ios::beg);
while (InOut.read((char*)&pp,sizeof pp))
{
cout<<count++<<":"<<setw(num)<<pp.name<<":"
<<setprecision(0)<<setw(12)<<pp.age
<<setprecision(2)<<setw(16)<<pp.pay<<endl;
}
InOut.close();
cout<<"完成\n";
return 0;
}
其中的InOut调用read函数为什么可以把PP,PP1,PP2读出来,
作者: guomin5213 发布时间: 2011-11-29
read()和cin相似啊,只不过是从文件中读取。第二个参数表示一次读取的缓冲区大小
作者: nuaazdh 发布时间: 2011-11-30
msdn看一下方法
作者: a707000646 发布时间: 2011-11-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28