一>

 #include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std; class Rectangle
{
private:
double length, width;
public:
Rectangle(double l, double w);
}; Rectangle::Rectangle(double l, double w) :length(l), width(w)
{} int main()
{
Rectangle rect(20.0, 10.3);
ofstream out("a.dat", ios_base::binary); //文件的输出
if (out.is_open()) //如果打开成功
{
out.write((char*)&rect, sizeof(Rectangle));//把Rectangle类的rect成员写入
out.close(); //注意:此步一定不能省
}
char* buf = new char[sizeof(Rectangle)]; //缓冲区
ifstream in("a.dat", ios_base::binary); //文件的写入
if (in.is_open()) //如果文件打开成功
{
in.read(buf, sizeof(Rectangle)); //从in指定的已打开文件中读取Rectangle字节到buf中
in.close(); //注意:此步一定不能省
}
return ;
}

结果(注意:.dat文件要用WinHex打开,网上有下载):

二>

 #include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std; class Rectangle
{
private:
double length, width;
public:
Rectangle(double l, double w);
void displayArea() { cout << length * width << endl; }
}; Rectangle::Rectangle(double l, double w) :length(l), width(w)
{} int main()
{
int a[] = { ,,, };
Rectangle rect(20.0, 10.3);
ofstream out("a.dat", ios_base::binary);
if (out.is_open())
{
out.write((char*)&rect, sizeof(Rectangle));
out.write((char*)a, * sizeof(int)); //对数组来说,数组有n个元素那么后面就是n*sizeof(int)
out.close();
}
char* buf = new char[sizeof(Rectangle)];
char* buf2 = new char[ * sizeof(int)];
ifstream in("a.dat", ios_base::binary);
if (in.is_open())
{
in.read(buf, sizeof(Rectangle));
in.read(buf2, sizeof(int) * );
int *a = new int[];
memcpy(a, buf2, * sizeof(int));
cout << a[] << endl;
delete[] a;
in.close(); Rectangle* pr = (Rectangle*)buf;
pr->displayArea();
}
delete[] buf;
return ;
}

结果:

三>

 #include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std; class Rectangle
{
private:
double length, width;
public:
Rectangle(double l, double w);
void displayArea() { cout << length * width << endl; }
}; Rectangle::Rectangle(double l, double w) :length(l), width(w)
{} int main()
{
fstream file("a.txt", ios_base::out | ios_base::app);
if (file.is_open())
{
file << "Hello world!";
file.close(); }
file.open("a.txt", ios_base::in);
if (file.is_open())
{
while (file.good())
{
string words;
file >> words;
cout << words << " ";
}
file.close();
cout << endl;
}
return ;
}

结果:(文件中与命令提示框输出得一样哦)

最新文章

  1. Google 开源技术protobuf
  2. Android Studio项目转Eclipse项目
  3. 总结CSS3新特性(媒体查询篇)
  4. iOS事件响应链
  5. dyld: Symbol not found: _OBJC_CLASS_$_NSURLSessionDataTask
  6. 代码中特殊的注释技术——TODO、FIXME和XXX的用处
  7. c# dataGridview的Cellclick移除事件
  8. SU Demos-04Deconvolution-02Wiener_Levinson
  9. [转]Not enough free disk space on disk &#39;/boot&#39;
  10. 瞬间从IT屌丝变大神——命名规则
  11. 错误:[将截断字符串或二进制数据。\r\n语句已终止。]
  12. I2C分析三
  13. wordpress博客近期变慢之解决(fonts.google.com)
  14. 以helloworld为例讲解magento中控制器的工作
  15. Sass与Compress实战:第八章
  16. LeetCode算法题-Find Pivot Index(Java实现)
  17. 0.Git介绍
  18. 使用Node.js搭建数据爬虫crawler
  19. jQuery之导航菜单(点击该父节点时子节点显示,同时子节点的同级隐藏,但是同级的父节点始终显示)
  20. map内置函数分析所得到的思路

热门文章

  1. C语言运算符优先级和结合性
  2. shell重温---基础篇(shell数组&amp;数组操作)
  3. result returns more than one elements此种错误,解决
  4. 【java并发编程实战】第八章:线程池的使用
  5. Ajax请求被缓存的几种处理方式
  6. ardupilot_gazebo仿真(四)
  7. 用IIS防止mdb数据库被下载(转载)
  8. 简单java采集程序一
  9. 正则表达式之旅_sed_awk
  10. 算法(12)Pascal&#39;s Triangle II