使用read(),write(),seekg(),seekp()实现二进制方式文件随机存取
// binary.cpp -- binary file I/O
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> //for exit()
using namespace std; const char * file = "planets.dat"; struct planet
{
char name[]; //name of planet
double population; //its population
double g; //its acceleration of gravity
};
inline void eatline()
{
while(cin.get() != '\n')
{
continue;
}
} int main()
{
planet pl;
cout << fixed << right; //show initial contents
ifstream fin; fin.open(file, ios_base::in | ios_base::binary); //binary file
if(fin.is_open())
{
cout << "Here are the current contents of the " << file << " file:" << endl;
while(fin.read((char* )&pl, sizeof pl))
{
cout << setw() << pl.name << ": "
<< setprecision() << setw() << pl.population
<< setprecision() << setw() << pl.g << endl;
}
fin.close();
} //add new data
ofstream fout;
fout.open(file, ios_base::out | ios_base::app | ios_base::binary);
if(!fout.is_open())
{
cerr << "Can't open " << file << " file for output:" << endl;
exit(EXIT_FAILURE);
}
cout << "Enter planet name (enter a blank line to quit):" << endl;
cin.get(pl.name, );
while(pl.name[] != '\0')
{
eatline();
cout << "Enter planet's population: ";
cin >> pl.population;
cout << "Enter planet's acceleration of gravity: ";
cin >> pl.g;
eatline();
fout.write((char *)&pl, sizeof pl);
cout << "Enter planet name (enter a blank line to quit):" << endl;
cin.get(pl.name, );
}
fout.close(); //show revised file
fin.clear();
fin.open(file, ios_base::in | ios_base::binary);
if(fin.is_open())
{
cout << "Here are the new contents of the " << file << " file:" << endl;
while(fin.read((char *)&pl, sizeof pl))
{
cout << setw() << pl.name << ": "
<< setprecision() << setw() << pl.population
<< setprecision() << setw() << pl.g << endl;
}
fin.close();
}
cout << "Done." << endl; return ;
}
//random.cpp -- random access to a binary file
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std; const int LIM = ;
const char * file = "planets.dat"; struct planet
{
char name[LIM]; //name of planet
double population; //its population
double g; //its acceleration
};
inline void eatline()
{
while(cin.get() != '\n')
{
continue;
}
} int main()
{
planet pl;
cout << fixed; //show initial contents
fstream finout; //read and write streams
finout.open(file, ios_base::in | ios_base::out | ios_base::binary); int ct = ;
if(finout.is_open())
{
finout.seekg(); //go to beginning
cout << "Here are the current contents of the " << file << " file:" << endl;
while(finout.read((char *)&pl, sizeof pl))
{
cout << ct++ << ": " << setw(LIM) << pl.name << ": "
<< setprecision() << setw() << pl.population
<< setprecision() << setw() << pl.g << endl;
}
if(finout.eof())
{
finout.clear(); //clean eof flag
}
else
{
cerr << "Error in reading " << file << " file." << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << file << " could not be opened -- bye." << endl;
exit(EXIT_FAILURE);
} //change a record
cout << "Enter the record number you wish to change: ";
long rec;
cin >> rec;
eatline(); //get rid of newline
if(rec < || rec >= ct)
{
cerr << "Invalid record number -- bye" << endl;
exit(EXIT_FAILURE);
}
streampos place = rec * sizeof pl; //convert to streampos type
finout.seekg(place); //random access
if(finout.fail())
{
cerr << "Error on attempted seek" << endl;
exit(EXIT_FAILURE);
} finout.read((char *)&pl, sizeof pl);
cout << "Your selection: " << endl;
cout << rec << ": " << setw(LIM) << pl.name << ": "
<< setprecision() << setw() << pl.population
<< setprecision() << setw() << pl.g << endl;
if(finout.eof())
{
finout.clear(); //clear eof flag
} cout << "Enter planet name: ";
cin.get(pl.name, LIM);
eatline();
cout << "Enter planetary population: ";
cin >> pl.population;
cout << "Enter planet's acceleration of gravity: ";
cin >> pl.g;
finout.seekp(place); //go back
finout.write((char *)&pl, sizeof pl) << flush;
if(finout.fail())
{
cerr << "Error on attempted write" << endl;
exit(EXIT_FAILURE);
} //show revised file
ct = ;
finout.seekg(); //go to beginning of file
cout << "Here are the new contents of the " << file << " file:\n";
while(finout.read((char *)&pl, sizeof pl))
{
cout << ct++ << ": " << setw(LIM) << pl.name << ": "
<< setprecision() << setw() << pl.population
<< setprecision() << setw() << pl.g << endl;
}
finout.close();
cout << "Done." << endl; return ;
}
使用read(),write(),seekg(),seekp()实现二进制方式文件随机存取的更多相关文章
- C语言采用文本方式和二进制方式打开文件的区别分析
稍微了解C程序设计的人都知道,文本文件和二进制文件在计算机上面都是以0,1存储的,那么两者怎么还存在差别呢?对于编程人员来说,文本文件和二进制文件就是一个声明,指明了你应该以什么方式(文本方式/二进制 ...
- C++文件操作之 seekg/seekp/tellg/tellp
问题描述: C++文件操作之 tellg/tellp/seekg/seekp 的使用 问题解决: (1)seekg/tellg/seekp/tellp 使用 tellp用于ostream调用,用来&q ...
- C++结构体对象数组的二进制方式读写
以一个学生信息的结构体数组为例. #include<iostream>#include<string>#include<fstream>using namespac ...
- 二进制方式快速安装MySQL数据库命令集合
二进制方式快速安装MySQL数据库命令集合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1.安装mysql ls mysql ...
- OleContainer操作Excel以二进制方式读写数据库
需求源头:OleContainer操作Excel,想把Excel以二进制方式存入数据库,并且以二进制方式读取存入流:Procedure SaveToStream(ADOTable1: TAdoTabl ...
- Elastic Stack之ElasticSearch分布式集群二进制方式部署
Elastic Stack之ElasticSearch分布式集群二进制方式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必大家都知道ELK其实就是Elasticsearc ...
- seekg()/seekp()与tellg()/tellp()的用法详解
本文转载于:http://blog.csdn.net/mafuli007/article/details/7314917 (在tcp的文件发送部分有应用) 对输入流操作:seekg()与tellg() ...
- c++ 二进制方式读取文件 读取特殊类型数据
#include <iostream> #include <fstream> using namespace std; /* 二进制方式进行读写文件,可以读写 各种各样数据类型 ...
- CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59
系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...
随机推荐
- iOS 开发-单元测试
前言 维基百科对单元测试的定义如下: 在计算机编程中,单元测试(英语:Unit Testing)又称为模块测试, 是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可 ...
- WebView使用详解(一)——Native与JS相互调用(附JadX反编译)
念念不忘,必有回响,永远坚持你所坚持的! 一直在用WebView,还没有系统的总结过它的用法,下面就系统的总结下,分享给大家 一.基本用法 1.加载在线URL void loadUrl(String ...
- My way to Python - Day05 - 面向对象
思维导图
- Android 项目建立步骤
使用eclipse,进行安卓开发,在建立项目的时候,有些步骤必须注意的, 本文就是对使用eclipse进行android开发的简单说明: 一.模拟器配置设定 使用eclipse开发安卓,需要用到and ...
- codevs4189字典(字典树)
/* 本字典树较弱 只支持插入单词 查询单词. 特殊的 bool变量w 标记此字母是不是某个单词的结束 (然而这个题并没卵用) */ #include<iostream> #include ...
- <html:form>、 <html:text>、<html:password>、<html:submit> 标签
用Struts标签来写表单元件, 引用: <%@ taglib uri="/tags/struts-html" prefix="html" %> 例 ...
- cxf客户端代码wsdlLocation设置相对路径
利用工生成的cxf客户端代码,wsdlLocation都是绝对路径,为了便于项目更加灵活管理,我们可以将该路径设置为相对路径: 1.下面图片是我的项目路径图片及wsdl地址存放路径: 2.下面图片是我 ...
- 将对象保存至文件——CArchive
CArchive允许以一个二进制的形式保存一个对象的复杂网络,也可以再次装载它们,在内存中重新构造,这一过程叫作串行化/序列化(Serialization),简单的说,CArchive与CFile配合 ...
- javaScript高程第三版读书笔记
看完<dom编程艺术>现在准备读进阶版的js高程了,由于篇幅较长,所以利用刚看完<dom编程艺术>学到的知识写了段JavaScript代码,来折叠各章的内容.并且应用到了< ...
- 织梦dedecms网站六大SEO优化技巧(转帖)
一个排名好的网站离不开好的cms,当然不同cms各有各的好处,因此我们在上线新网站的时候,要针对不同的情况因地制宜,选择不同的网站管理系统来做seo优化,现在使用比较流行的cms是织梦dedecms, ...