c++ fstream用法(2)
一>
#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)的更多相关文章
- c++ fstream用法
今天介绍一个复制 粘贴的函数,用fstream实现 #include "stdafx.h" #include<iostream> #include<fstream ...
- C++ fstream 用法
#include <fstream> #include <iostream> main() { int a,b,c,d; std::ifstream infile (" ...
- 转载:fstream和ifstream详细用法
文件 I/O 在C++中比烤蛋糕简单多了.在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的. 一.ASCII 输出 为了使用下面的方法, ...
- fstream的用法
本文转载自 http://www.newxing.com/Tech/Program/Cpp/577.html C++文件流:fstream // 文件流ifstream // 输入文件流ofst ...
- fstream,ifstream,ofstream 详解与用法
fstream,istream,ofstream 三个类之间的继承关系 fstream :(fstream继承自istream和ofstream)1.typedef basic_fstream< ...
- c++ fstream中seekg()和seekp()的用法
转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...
- C++ REST SDK的基本用法
微软开发了一个开源跨平台的http库--C++ REST SDK(http://casablanca.codeplex.com/),又名卡萨布兰卡Casablanca,有个电影也叫这个名字,也许这个库 ...
- C++ string 用法详解
/////////////////////////////////////////////////////////////////////////////////// 任何人对本文进行引用都要标明作者 ...
- C++ IO 详细用法
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处 本文将分别从<iostream>,<sstream>,<fstream> ...
随机推荐
- 图片验证码给AI使用
为了破解图形验证码,AI需要大量的图片数据.为了简单获取大量的图形来喂给Ai模型训练,索性自己写一把.代码来一发.. import java.awt.Color; import java.awt. ...
- DSP28335的XINTF操作SRAM实验
1. 本次使用三兄弟的XDS28335开发板,研究一下XINTF操作SRAM的代码.哈弗结构,奇怪,DSP28335是哈弗结构,那么数据和程序空间应该独立的,为啥书上说采用统一的寻址方式?估计只是读写 ...
- Prometheus 普罗米修斯监控
周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下 这里只介绍客户端的配置 1:首先在POM中添加依赖 <dependency> <groupI ...
- CF 55D
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...
- 贝叶斯网(1)尝试用Netica搭建简单的贝叶斯网并使用贝叶斯公式解释各个bar的结果
近来对贝叶斯网十分感兴趣,按照博客<读懂概率图模型:你需要从基本概念和参数估计开始>给出的第一个例子,试着搭建了一个student网. (1)点击绿F,对条件概率表予以输入(包括两个祖先节 ...
- 腾讯云,搭建 FTP 文件服务
腾讯云,搭建 FTP 文件服务 腾讯云,搭建 FTP 文件服务 安装并启动 FTP 服务 任务时间:5min ~ 10min 安装 VSFTPD 使用 yum 安装 vsftpd: yum insta ...
- swarm 服务器安装
apt install docker.io-----------------------配置加速器.私有仓库地址---------------------mkdir -p /etc/dockertee ...
- 测试理论- the conten of test plan
1 testing objects 测试对象 2 testing scope 测试范围 3 testing the frame (?) 4 the environment 5 reason for t ...
- JavaScript - arguments object
The arguments object is an Array-like object corresponding to the arguments passed to a function. fu ...
- 软工实践 - 第二十七次作业 Beta 冲刺(5/7)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10134471.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...