怎样从二进制文件中读出double类型数据??

daocaoren930 2007-11-06 02:41:19
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
struct
{
double salary;
char name[23];

}employee1;
employee1.salary=8000;
strcpy(employee1.name,"L Zheng");
ofstream outfile("payroll",ios_base::binary);
outfile.write((char *) &employee1,sizeof(employee1));
outfile.close();

char ch;
ifstream tfile("payroll",ios_base::binary);
if(tfile)
{
//cout<<tfile.tellg()<<endl;
tfile.seekg(8);
//cout<<tfile.tellg()<<endl;
while(tfile.good())
{
//double b;
// cout<<"tellg():"<<tfile.tellg()<<endl;
//tfile.read((char*) &b,sizeof(double));
//cout<<b<<endl;
tfile.get(ch);
if(!ch) break;
cout<<ch;
}
}
else
{
cout<<"ERROR:Cannot open file 'payroll'."<<endl;
}
tfile.close();
return 0;
}

怎样从二进制文件中读出salary(double类型的)的值呢?
...全文
259 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaqiangscut00 2007-11-07
  • 打赏
  • 举报
回复
我也觉得 直接写入double的值,然后按二进制方式读出来,根据字节大小转换成double就行了
jixingzhong 2007-11-07
  • 打赏
  • 举报
回复
怎么写的,怎么读取,不需要考虑太多的细节
hziee_ 2007-11-06
  • 打赏
  • 举报
回复
首先要搞清楚double的内存存储格式。google一下吧
lenux 2007-11-06
  • 打赏
  • 举报
回复
你直接按二进制方式写入double的值,然后按二进制方式读出来,根据字节大小转换成double就行了

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧