使用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 ...
随机推荐
- 第四篇:SQL
前言 确实,关于SQL的学习资料,各类文档在网上到处都是.但它们绝大多数的出发点都局限在旧有关系数据库里,内容近乎千篇一律.而在当今大数据的浪潮下,SQL早就被赋予了新的责任和意义. 本篇中,笔者将结 ...
- android应用的不同版本间兼容性处理
在Android系统中向下兼容性比较差,但是一个应用APP经过处理还是可以在各个版本间运行的.向下兼容性不好,不同版本的系统其API版本也不同,自然有些接口也不同,新的平台不能使用旧的API,旧的平台 ...
- 二分图最大匹配(匈牙利算法Dfs模板)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2020 using name ...
- git 的记住用户名和密码和一些常用
git config --global core.autocrlf falsegit config --global color.ui truegit config --global credenti ...
- DEDECMS批量修改默认文章和列表命名规则的方法
很多人因为添加分类而苦恼,尤其是批量添加的时候,必须要重新修改一下文章命名规则和列表命名规则,都是为了做SEO.如果进行默认值的修改,就会事半功倍.不多说. 一.DEDE5.5修改默认文章命名规则. ...
- html.day01
1.web标准: 1. 结构 (xhtml) 2. 表现(css) 3.行为(js) html 超文本标记语言 xhtml (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...
- mssql sql高效关联子查询的update 批量更新
/* 使用带关联子查询的Update更新 --1.创建测试表 create TABLE Table1 ( a varchar(10), b varchar(10), ...
- Linux sed命令在指定行前后添加内容
一.在匹配行前后加内容在包含www.baidu.com的行前面或后面添加多一行内容www.qq.com#匹配行前加sed -i '/www.baidu.com/i www.qq.com' domain ...
- SQL批量信息保存(XML格式字符串数据)
/* *功能:SQL批量信息录入 *此存储过程获取表单信息,插入表中.*/CREATE PROC [dbo].[sp_SaveToMX1]@XML text --明细表XML字符串信息ASBEG ...
- SQL中varchar和nvarchar有什么区别?
varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节.nvarc ...