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> ...
随机推荐
- 签名的html
<b><a href="http://www.feiyuanxing.com" style="color:red">未来星开发团队--狒 ...
- How to Upload multiple files to documentLibrary in one time
In a Sharepoint 2013 website,we can upload one file to the documentlibrary by click "Uploa ...
- 使用Visual Studio 2017构建.Net Core的Docker镜像
1 Docker 镜像优化 微软在为开发人员生成 Docker 镜像时,提供以下三种主要方案: 用于开发 .NET Core 应用的 镜像 用于构建生成 .NET Core 应用的 镜像 用于运行 ...
- Accept 惊群现象测试perl脚本
$uname -a Linux debian-11-34 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24) x86_64 G ...
- 孤荷凌寒自学python第七十一天开始写Python的第一个爬虫
孤荷凌寒自学python第七十一天开始写Python的第一个爬虫 (完整学习过程屏幕记录视频地址在文末) 在了解了requests模块和BeautifulSoup模块后,今天开始真正写一个自己的爬虫代 ...
- [GraphSAGE] docker安装与程序运行
安装Docker与程序运行 1. requirements.txt Problem: Downloading https://files.pythonhosted.org/packages/69/cb ...
- python安装Django
现在有很多建站系统,很多都是基于php的,比如WordPress. 而Django 是老牌基于Python的CMS框架了,一直听说很强大,甚至曾经很红的Ruby On Rails都参考了它的很多概念, ...
- Mapper的方式总结
Mapper的方式总结: <mappers> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <package name=&quo ...
- linux备忘录-正则表达式与文件格式化处理
正则表达式 POSIX标准的符号 [:alnum:] -> 英文大小写字母和数字 0-9,A-Z,a-z [:alpha:] -> 英文大小写字母 A-Z,a-z [:blank:] -& ...
- [译]如何根据条件从pandas DataFrame中删除不需要的行?
问题来源:https://stackoverflow.com/questions/13851535/how-to-delete-rows-from-a-pandas-dataframe-based-o ...