一>

 #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. Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping

    题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...

  2. Tensorflow之安装GPU版错误集合

        在根据教程http://blog.csdn.net/sb19931201/article/details/53648615安装好全部的时候,却无情的给我抛了几个错: 1.AttributeEr ...

  3. linux c 出错集

    2018.7.8 1.声明结构体时,结构体内部不能赋值.比如 struct student{ char id=0; char score=0; }; 这样大错特错! 2.字符数组 char a[10] ...

  4. Mysql 启动运行

    MYSQL默认端口:3306用户: root   也可自己添加用户启动数据库服务名: MYSQL (在安装的时候会设置) 在开始菜单栏->附件->右键命令提示符->以管理员身份运行: ...

  5. 多表头的DataGridView

           上次在程序中要用到多表头的DataGridView,在网上搜索了一个,感觉还不错,现在简单的介绍一下它的用法.首先得把这个dll拷贝到相应的目录下,dll名称是myMultiColHea ...

  6. fastlane自动化打包ipa并发布到firim或者蒲公英

    1.打开终端,确保Xcode Command Line Tools 安装了最新版 xcode-select --install 2.安装fastlane sudo gem install -n /us ...

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

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

  8. Scala学习笔记(一):环境搭建

    计算机领域的编程语言种类繁多,如C.C++.Java.C#等,我们知道的一般都是较为流行的编程语言,然有更多的是没听说过的,于是也就说不上关注或者使用了 一次在网上查资料时,无意间看到了“函数式编程” ...

  9. 最火的.NET开源项目[转]

    综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...

  10. 2.Linux文件和目录

    1. 目录和路径 linux下比较特殊的目录: . 代表此层目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表『目前使用者身份』所在的home目录 ~account 代表 account 这 ...