首页 新闻 会员 周边

关于c++存储文件的疑问

0
悬赏园豆:50 [待解决问题]

下面是我的代码,代码内容是新建并以二进制打开d盘下的a.txt文件,向该文件写入一个double型的5.6数值,关闭文件。再以二进制读的方式打开文件,从文件读出三个字节并显示,显示结果为“5.6”。问题:double型应该是占8个字节,为什么我读三个字节就把该值显示出来了??

#include "stdafx.h"

#include <iostream>

#include <fstream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])

{

string fileName("D:\\a.txt");

ofstream fout(fileName.c_str(), ios::binary);

if(!fout)

{

cout<<"Can't open this file"<<endl;

return 0;

}
double a = 5.6;

fout<<a;

fout.close();
ifstream fin(fileName.c_str(), ios::binary);

char cha[4];

cha[3] = 0;

fin.read(cha ,3);

cout<<cha<<endl;

fin.close();

return 0;

}

doubleHH的主页 doubleHH | 初学一级 | 园豆:150
提问于:2011-05-15 11:00
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册