write()二进制方式写文件
来源:互联网 发布:淘宝速成教程 编辑:程序博客网 时间:2024/06/13 03:48
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a[10]={0};
for(int i=0;i<9;i++)
{
a[i+1]=a[i]*10+i+1;
cout<<a[i]<<endl;
}
ofstream rs("ok2002com.txt");
ofstream rs2("ok2002com.bin",ios::binary);
for(int i=0;i<9;i++)
{
rs<<a[i]<<" ";
rs2.write((char*)(&a[i]),sizeof(a[i]));
cout<<"rs.tellp("<<i<<")="<<rs.tellp()<<",";
cout<<"rs2.tellp("<<i<<")="<<rs2.tellp()<<endl;
}
rs.close();
rs2.close();
//cin>>i;
#include<fstream>
using namespace std;
int main()
{
int a[10]={0};
for(int i=0;i<9;i++)
{
a[i+1]=a[i]*10+i+1;
cout<<a[i]<<endl;
}
ofstream rs("ok2002com.txt");
ofstream rs2("ok2002com.bin",ios::binary);
for(int i=0;i<9;i++)
{
rs<<a[i]<<" ";
rs2.write((char*)(&a[i]),sizeof(a[i]));
cout<<"rs.tellp("<<i<<")="<<rs.tellp()<<",";
cout<<"rs2.tellp("<<i<<")="<<rs2.tellp()<<endl;
}
rs.close();
rs2.close();
//cin>>i;
}
●文本方式存储与二进制方式存储的一些区别:1、文本方式存储每个数据在文件中的长度不等,所占的字节数与数据的位数有关,数据间必须用分隔符(如空格)隔开;2、二进制方式存储每个数据在文件中的长度相同,所占字节数仅与数据类型有关,数据间不需要加入分隔符。●二进制读写文件的优点(1)便于对数据进行随机访问;(2)存取效率高;(3)可以对文件进行压缩处理;(4)存储密度大;(5)数据文件便于保密;●二进制读写文件需了解:(1)二进制读写文件兼容性低;(2)二进制读写文件,不能用字处理程序进行阅读、编辑或修改;●到底用文本方式存储较好?还是用二进制方式存储较好?通常将文本信息以文本文件方式存储,而将数值信息以二进制方式存储。●什么是文本信息?什么是数值信息?文本信息:电话号码、新闻、邮政编码等等;共同属性:数字不参与计算;数值信息:产品销售价格、公司总资产、公司年纯收入等等。共同属性:数字参与计算;需注意:文本信息与数值信息没有绝对的界线,二者可能会相互转换,如:仅仅公布公司季度获利情况,可存储为文本信息,因为其中的金额此次不用于运算。*/
c++中write是用于向文件中写数据的函数。
函数原型:ostream& write (const char* s, streamsize n);
参数:s是数据源指针,n表示字节数
返回值:返回 ostream 对象的引用 (*this).
注意:使用需要#include <fstream>
实例:
#include <fstream> // std::ifstream, std::ofstr
// Copy a file
#include <fstream> // std::ifstream, std::ofstream
int main () {
std::ifstream infile ("test.txt",std::ifstream::binary);
std::ofstream outfile ("new.txt",std::ofstream::binary);
// get size of file
infile.seekg (0,infile.end);
long size = infile.tellg();
infile.seekg (0);
// allocate memory for file content
char* buffer = new char[size];
// read content of infile
infile.read (buffer,size);
// write to outfile
outfile.write (buffer,size);
// release dynamically-allocated memory
delete[] buffer;
outfile.close();
infile.close();
return 0;
}
函数原型:ostream& write (const char* s, streamsize n);
参数:s是数据源指针,n表示字节数
返回值:返回 ostream 对象的引用 (*this).
注意:使用需要#include <fstream>
实例:
#include <fstream> // std::ifstream, std::ofstr
// Copy a file
#include <fstream> // std::ifstream, std::ofstream
int main () {
std::ifstream infile ("test.txt",std::ifstream::binary);
std::ofstream outfile ("new.txt",std::ofstream::binary);
// get size of file
infile.seekg (0,infile.end);
long size = infile.tellg();
infile.seekg (0);
// allocate memory for file content
char* buffer = new char[size];
// read content of infile
infile.read (buffer,size);
// write to outfile
outfile.write (buffer,size);
// release dynamically-allocated memory
delete[] buffer;
outfile.close();
infile.close();
return 0;
}
0 0
- write()二进制方式写文件
- java8 write file 写文件
- 【文件管理】文件的写write()
- 3.8 write函数-文件数据写
- FileWriter write 写文件缺失问题
- dom写一个write xml文件
- c++ 二进制方式读写文件
- Java:二进制方式读取文件
- 二进制写文件 多出0D
- C++写二进制数据到文件中
- f.write()写文件,结果文件却是空的
- Python基础-IO编程-读文件read-写文件write
- vbscript脚本用二进制方式读写文件
- 以二进制方式打开文件并显示
- c#-以二进制方式读文件
- 浏览器以二进制流方式读取文件
- 将整型以二进制方式写入文件
- Python 读写文件---pickle 二进制方式读写
- 框架 的分页
- 在报表中给session赋值实现报表间参数共享
- 用链表实现二叉树
- 莫让“专业对口”俘虏了你
- 自带背景音乐的代码
- write()二进制方式写文件
- java异常整理
- 星座运势sdk代码示例实现分享
- Error:getsupportfragmentmanager()未定义
- ofbiz之entity 实体解析 扩展 视图 复合列写法
- JSP自定义标签开发入门
- Android事件处理的两种模型
- 实施质量保证和控制质量的区别
- VMware下安装Centos-配置LNMP环境全过程记录