使用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 ...
随机推荐
- Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
- 关于Daydream VR的最直白的介绍
虚拟现实(Virtual Reality),简称虚拟技术,也称虚拟环境,是利用电脑模拟产生一个三度空间的虚拟世界,提供用户关于视觉等感官的模拟,让用户如同身历其境一般,电脑可以立即进行复杂的运算,将精 ...
- Linux FTP YUM源搭建简单记录
1. 挂载iso镜像,进入Packages目录下安装FTP服务 #rpm -ivh vsftpd-3.0.2-10.el7.x86_64.rpm 2. 修改vsftpd配置文件如下 [root@rus ...
- zoj 3537 Cake(区间dp)
这道题目是经典的凸包的最优三角剖分,不过这个题目给的可能不是凸包,所以要提前判定一下是否为凸包,如果是凸包的话才能继续剖分,dp[i][j]表示已经排好序的凸包上的点i->j上被分割成一个个小三 ...
- 教学项目之-通过Python实现简单的计算器
教学项目之-通过Python实现简单的计算器 计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...
- Traceview 性能分析工具
简介 TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method.详细内容参考:http://deve ...
- 【开源java游戏框架libgdx专题】-05-模块描述与上下文
模块描述(Modules overview) Input:为所有的平台提供一个统一的输入模型和处理程序. 获取触摸示例: if (Gdx.input.isTouched()) { System.out ...
- Jquery分步学习一
<script type="text/javascript" src="../js/jquery-1.11.0.js"></script> ...
- jquery Tab默认情况下自动切换
<!DOCTYPE html><html lang="zh-CN"><head><meta http-equiv="Conten ...
- Linq101-Partitioning
using System; using System.Linq; namespace Linq101 { class Partitioning { /// <summary> /// Th ...