chapter17文件

chapter17文件
#1.文本 和二进制
实现增删改查文件的二进制和文本呢格式转换

// random.cpp -- random access to a binary file
#include <iostream>     // not required by most systems
#include <fstream>
#include <iomanip>
#include <cstdlib>      // (or stdlib.h) for exit()
const int LIM = 20;
struct planet
{
    char name[LIM];      // name of planet
    double population;  // its population
    double g;           // its acceleration of gravity
};
const char * file = "guests.dat";  // ASSUMED TO EXIST (binary.cpp example)
inline void eatline() { while (std::cin.get() != '\n') continue; }
int main()
{
    using namespace std;
    planet pl;
    cout << fixed;
// 显示
    fstream finout;     // read and write streams
    finout.open(file, ios_base::in | ios_base::out | ios_base::binary);//NOTE: Some Unix systems require omitting | ios::binary
    int ct = 1;
    if (finout.is_open())
    {
        //finout.seekg(0);    // go to beginning
        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(); // clear eof flag
        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();// get rid of newline
	streampos place;
    if (rec <= 0)
    {
        cout<< "not fix text -- bye\n";
    }
	else 
	{
		if (finout.fail())
		{
			cerr << "Error on attempted seek\n";
		}
		if (rec <= (ct - 1))//修改现有
		{
			place = rec * sizeof pl;  // convert to streampos type
			finout.seekg(place);    // random access
			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();     // clear eof flag
			finout.seekp(place);    // go back	
		}
		else//添加新的
		{
			cout << "you want add" << endl;
		}
		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.write((char *)&pl, sizeof pl) << flush;
	}
	//转换
	const char * fileout = "guestsout.dat";
	ofstream fconveout(fileout, ios::out | ios::app);
	finout.seekg(0);
	while (finout.read((char *)&pl, sizeof pl))
	{	
		fconveout << pl.name << " " << pl.population << " " << pl.g << "\n";
	}
	fconveout.close();
	//读取文本并显示
	ifstream fin;
	fin.clear();    // not necessary for some compilers
	fin.open(file);
	if (fin.is_open())
	{
		cout << "Here are the conve contents of the "
			<< fileout << " file:\n";
		while (fin.read((char *)&pl, sizeof pl))
		{
			cout << pl.name << " " << pl.population << " " << pl.g << "\n";
		}
		fin.close();
	}
    cout << "Done.\n";
// keeping output window open
    cin.clear();
    eatline();
    cin.get();
    return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值