// 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()实现二进制方式文件随机存取的更多相关文章

  1. C语言采用文本方式和二进制方式打开文件的区别分析

    稍微了解C程序设计的人都知道,文本文件和二进制文件在计算机上面都是以0,1存储的,那么两者怎么还存在差别呢?对于编程人员来说,文本文件和二进制文件就是一个声明,指明了你应该以什么方式(文本方式/二进制 ...

  2. C++文件操作之 seekg/seekp/tellg/tellp

    问题描述: C++文件操作之 tellg/tellp/seekg/seekp 的使用 问题解决: (1)seekg/tellg/seekp/tellp 使用 tellp用于ostream调用,用来&q ...

  3. C++结构体对象数组的二进制方式读写

    以一个学生信息的结构体数组为例. #include<iostream>#include<string>#include<fstream>using namespac ...

  4. 二进制方式快速安装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 ...

  5. OleContainer操作Excel以二进制方式读写数据库

    需求源头:OleContainer操作Excel,想把Excel以二进制方式存入数据库,并且以二进制方式读取存入流:Procedure SaveToStream(ADOTable1: TAdoTabl ...

  6. Elastic Stack之ElasticSearch分布式集群二进制方式部署

    Elastic Stack之ElasticSearch分布式集群二进制方式部署 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必大家都知道ELK其实就是Elasticsearc ...

  7. seekg()/seekp()与tellg()/tellp()的用法详解

    本文转载于:http://blog.csdn.net/mafuli007/article/details/7314917 (在tcp的文件发送部分有应用) 对输入流操作:seekg()与tellg() ...

  8. c++ 二进制方式读取文件 读取特殊类型数据

    #include <iostream> #include <fstream> using namespace std; /* 二进制方式进行读写文件,可以读写 各种各样数据类型 ...

  9. CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59

    系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...

随机推荐

  1. [转] TCP数据包重组实现分析

    PS: 这个实现对于某些特定情况未必是最佳实现,可以用数组来代替队列来实现 参照TCP/IP详解第二卷24~29章,详细论述了TCP协议的实现,大概总结一下TCP如何向应用层保证数据包的正确性.可靠性 ...

  2. Referenced file contains errors (http://tiles.apache.org/dtds/tiles-config_3_0.dtd)

    java开发时遇到的问题,之前还是好好的,没有错误提示.可是今天一打开项目就出现这种问题.真不知道是怎么回事,在这里求助.错误如下: Referenced file contains errors ( ...

  3. 那些年不错的Android开源项目(转)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  4. Day6 - Python基础6 面向对象编程

    Python之路,Day6 - 面向对象学习   本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.     引子 你现在是一家游戏公司的开发 ...

  5. Activity 【生命周期】

    不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...

  6. 转载:C# 之泛型详解

    本文原地址:http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html.感谢博主分享! 什么是泛型 我们在编写程序时,经常遇到两个模 ...

  7. Have trouble in your life

    当你烦恼的时候不知道如何是好时,你可以下载此程序,可以帮助你化解烦恼! 下载地址: http://pan.baidu.com/s/1i3FtxHF

  8. mysql 优化点小结

    1.数据库表设计的合理性 1)三范式 一范式:原子性,属性不可分: 二范式:无部分依赖, 例:(学号, 课程名称) → (姓名, 年龄, 成绩, 学分),存在部分依赖 (学号) → (姓名, 年龄) ...

  9. CSP内容安全策略

    在浏览网页的过程中,尤其是移动端的网页,经常看到有很多无关的广告,其实大部分广告都是所在的网络劫持了网站响应的内容,并在其中植入了广告代码.为了防止这种情况发生,我们可以使用CSP来快速的阻止这种广告 ...

  10. wpf 控件复制 克隆

    方法1: string xaml = System.Windows.Markup.XamlWriter.Save(rtb1); RichTextBox rtb2 =System.Windows.Mar ...