/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘畅
* 完成日期:2015 年 6 月 13 日
* 版 本 号:v1.0
*
* 问题描述:阅读并运行下面两个程序,分别用记事本和二进制文件阅读器。查看其内容,并理解文件存储的原理
* 输入描述:输入一个数;
* 程序输出:文本输出:
(1)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int a;
ofstream outfile("f1.dat",ios::out);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cin>>a;
outfile<<a<<endl;
outfile.close();
return 0;
}
运行结果:
(2)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int a;
ofstream outfile("f2.dat",ios::out|ios::binary);
if (!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cin>>a;
outfile.write((char*)&a,sizeof(int));
outfile.close();
return 0;
}
运行结果:
学习心得:
对于第一种理解了,但是第二种还有问题,使用成员函数write写二进制文件,写入数值保存为乱码的原理是什么,通过二进制文件阅读器为何第一个显示的是ASCII码,第二个确完全不知道是什么了。