一>

 #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 ;
}

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

c++ fstream用法(2)的更多相关文章

  1. c++ fstream用法

    今天介绍一个复制 粘贴的函数,用fstream实现 #include "stdafx.h" #include<iostream> #include<fstream ...

  2. C++ fstream 用法

    #include <fstream> #include <iostream> main() { int a,b,c,d; std::ifstream infile (" ...

  3. 转载:fstream和ifstream详细用法

    文件 I/O 在C++中比烤蛋糕简单多了.在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的. 一.ASCII 输出 为了使用下面的方法, ...

  4. fstream的用法

    本文转载自 http://www.newxing.com/Tech/Program/Cpp/577.html   C++文件流:fstream // 文件流ifstream  // 输入文件流ofst ...

  5. fstream,ifstream,ofstream 详解与用法

    fstream,istream,ofstream 三个类之间的继承关系 fstream :(fstream继承自istream和ofstream)1.typedef basic_fstream< ...

  6. c++ fstream中seekg()和seekp()的用法

    转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...

  7. C++ REST SDK的基本用法

    微软开发了一个开源跨平台的http库--C++ REST SDK(http://casablanca.codeplex.com/),又名卡萨布兰卡Casablanca,有个电影也叫这个名字,也许这个库 ...

  8. C++ string 用法详解

    /////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者 ...

  9. C++ IO 详细用法

    http://www.cnblogs.com/keam37/ keam所有 转载请注明出处 本文将分别从<iostream>,<sstream>,<fstream> ...

随机推荐

  1. R语言学习笔记(十):零碎知识点(21-25)

    21--assign() assign函数可以通过变量名的字符串来赋值 > assign('a', 1:3) > a [1] 1 2 3 > b <- c('a') > ...

  2. 2019年第十届蓝桥杯C/C++程序设计本科B组省赛 E迷宫

    试题 E: 迷宫 本题总分: 分 [问题描述] 下图给出了一个迷宫的平面图,其中标记为 的为障碍,标记为 的为可 以通行的地方. 迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它 ...

  3. CSS3新特性回顾

    CSS3 介绍 开始实例 新特征简介 强大的CSS选择器 抛弃图片的视觉效果 盒模型变化(多列布局和弹性盒模型) 阴影效果 Web字体和web Font 图标 CSS33过渡与动画交互效果 媒体查询 ...

  4. h5调试工具

    1.Safari:iPhone 调试利器,查错改样式首选: 2.iOS 模拟器:不需要真机,适合调试 Webview 和 H5 有频繁交互的功能页面: 3.Charles: Mac OS 系统首选的抓 ...

  5. 扩展报表-JavaSet

    前言 使用商业分析中的扩展报表平台,可以很方便的进行数据分析,进行图表化直观展示.一般情况下使用SQL数据集进行SQL的编写,进而配合扩展报表平台进行数据分析图表的绘制,但SQL数据集针对固定的参数进 ...

  6. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  7. howto:在构建基于debian的docker基础镜像时,更换国内包源

    debian经常被用作构建应用镜像的基础镜像,如微软在构建linux下的dotnetcore基础镜像时,提供了基于debian 8(jessie)和debian 9(stretch)的镜像. 由于这些 ...

  8. Qt 使用irrlicht(鬼火)3D引擎

    项目中需要加载简单的3D场景.资深老前辈推荐使用开源小巧的引擎irrlicht. 关于irrlicht,来之百度百科 Irrlicht引擎是一个用C++书写的高性能实时的3D引擎,可以应用于C++程序 ...

  9. browsersync的安装与基本使用

    browser-sync启动命令 Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面. 官网文档:http://www.brows ...

  10. python程序设计——基本语言特性

    参考<Python程序设计(第2版)> 清华大学出版社 董付国 ##### 除特殊说明外,以下代码示例,均为python 3.6版本 ##### 一.Python语言特性 1.python ...