使用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 ...
随机推荐
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- servlet 配置到服务器
最近写了个安卓项目,服务端用的servlet.因为第一次写java项目,写完如何发布不是太清除,于是把这回经理写出来,一来做个记录,二来也给和我同样经历的朋友一点启示. 首先配置你的java主机和你的 ...
- codevs1044四子连棋(Dfs)
/* 数据范围太小 暴力暴力 Dfs直接 终止条件嘛 就是4中目标棋局 挨着枚举一遍就好了 搜索的起点一定是空格 当然 空格周围有黑有白 黑先走或者白先走答案可能不一样 所以 维护一个b 表示这一步走 ...
- ADO.NET基础
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 关于C#中get和set
在看书的时候看见了一段代码,有两个类person: public class person { public string name; } public class person { public s ...
- Spring.Net+NHibernate+asp.net mvc + easyui
毕业4个月的入手项目..前段时间在公司一直做的维护..为了弄明白自己也就跟着写了一个,目前也正在学习:不对的或者是有更好的还请各位赐教. 在学习的过程中主要参考::http://www.cnblogs ...
- 关于android应用闪屏的几种情况
1.主菜单进入某应用闪屏: 常见是一个空的activity作为launcher属性,实际上它什么事业没干,真正干事情的是从它通过intent启动的activity. 例子: public class ...
- REST和SOAP Web Service的比较
1.http://stevenjohn.iteye.com/blog/1442776 2.http://blog.csdn.net/cnyyx/article/details/7483766
- Swift - 31 - 常量参数, 变量参数和inout参数
//: Playground - noun: a place where people can play import UIKit // swift中默认情况下, 传入的参数是不可以修改的, 也就是l ...
- 理解javascript之 对象
大纲: 1.介绍attribute property的异同,翻译自http://javascript.info/tutorial/attributes-and-custom-properties#pr ...