C++文件操作典型案例
来源:互联网 发布:c语言开发实战宝典pdf 编辑:程序博客网 时间:2024/05/01 00:26
案例一:
1.打开文件,显示其中内容
2.向其中追加记录
3.显示追加之后的文件内容
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
inline void eatline() { while (std::cin.get() != '\n')continue; }
struct planet
{
char name[20];
double population;
double g;
};
const char* file = "planets.dat";
int main()
{
planet pl;
cout << fixed << right;
ifstream fin;
fin.open(file, ios_base::in | ios_base::binary);
if (fin.is_open())
{
cout << "Here are the current contents of the " << file << "file:\n";
while (fin.read((char*)&pl, sizeof pl))
{
cout << setw(20) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
fin.close();
}
ofstream fout(file, ios_base::out | ios_base::app | ios_base::binary);
if (!fout.is_open())
{
cerr << "Can't Open " << file << "fiel for output:\n";
exit(EXIT_FAILURE);
}
cout << "Enter plant name(Enter a blank line to quit):\n";
cin.get(pl.name, 20);
while (pl.name[0] != '\0')
{
eatline();
cout << "Enter planetary 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):\n";
cin.get(pl.name, 20);
}
fout.close();
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:\n";
while (fin.read((char*)&pl, sizeof pl))
{
cout << setw(20) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
fin.close();
}
cout << "Done!\n";
}
案例二:
1.打开文件显示其中内容
2.选择要修改的记录
3.显示选中的要修改的记录
4.修改记录
5.显示修改之后的内容
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
const int LIM = 20;
struct planet
{
char name[LIM];
double population;
double g;
};
const char* file = "Planet.dat";
inline void eatline() { while (cin.get() != '\n')continue; }
int main()
{
/*ofstream fout("Planet.dat");
fout.close();*/
planet pl;
cout << fixed;
fstream finout;
finout.open(file, ios_base::in | ios_base::out | ios_base::binary);
int ct = 0;
if (finout.is_open())
{
finout.seekg(0);
cout << "Here are the current contents of the " << file << "file:\n";
while (finout.read((char *)&pl,sizeof pl))
{
cout << ct++ << ": " << setw(LIM) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
if (finout.eof())
{
finout.clear();
}
else
{
cerr << "Error in reading" << file << ".\n";
exit(EXIT_FAILURE);
}
}
else
{
cerr << file << "could not be opened--bye.\n";
exit(EXIT_FAILURE);
}
cout << "Enter the record number you wish to change: ";
long rec;
cin >> rec;
eatline();
if (rec<0 || rec >= ct)
{
cerr << "Invalid record bumber --bye \n";
exit(EXIT_FAILURE);
}
streampos place = rec * sizeof pl;
finout.seekg(place);
if (finout.fail())
{
cerr << "Error on attempted seek\n";
exit(EXIT_FAILURE);
}
finout.read((char *)&pl, sizeof pl);
cout << "Your selection:\n";
cout << rec << ": " << setw(LIM) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
if (finout.eof())
{
finout.clear();
}
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);
finout.write((char*)&pl, sizeof pl) << flush;
if (finout.fail())
{
cerr << "Error on attempted write\n";
exit(EXIT_FAILURE);
}
ct = 0;
finout.seekg(0);
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(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
cout << "Done!\n";
return 0;
}
1 0
- C++文件操作典型案例
- linux 网络编程典型案例(proxy.c) - linux 操作
- 典型文件操作 (java.io.*)
- java文件操作案例
- 文件操作案例
- C/C++学习之C提高-----文件操作,C程序读写文件、配置文件读写案例
- 风险投资案例典型分析
- 典型一卡通节水案例
- 决策树的典型案例
- sql优化-典型案例
- Spark典型案例
- 网站典型故障案例
- C/C++: 操作符重载案例
- 文件操作案例-大文件加解密
- 使用JDBC连接MySQL数据库--典型案例分析(一)---员工数据读取与操作
- 使用JDBC连接MySQL数据库--典型案例分析(六)----实现账户转账操作
- [C/C++]文件操作
- Rational产品配置典型案例
- 从iTunes中下载手机应用,并获取应用图标
- C语言中,头文件和源文件的关系(转)
- samba 配置
- JavaSE008_循环结构之return
- Android View绘制流程
- C++文件操作典型案例
- Ubuntu 15.10下安装broadcom无线网卡驱动
- HTML中文出现乱码的解决办法
- Division Operators
- Activity中UI框架基本概念
- poj 1321 棋盘问题 题解
- 【c/c++】map
- zhu的随笔:2016的诗,2015的事
- C++内核格式化