⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p11_13.cpp

📁 相当丰富的C++源码
💻 CPP
字号:
/****************************************************
* 程序名:p11_13.cpp                                *
* 功能: 文件指针使用实例                           *
*                    By Antony                      *
*                     02-08-2005                    *
****************************************************/
# include <fstream>
# include <iostream>
using namespace std;
void main() {
	char name1[8],name2[8];
	float score1,score2;
	int rec_num,rec_size,i;
	fstream binfile("d:\\c++book\\record.dat",ios::out|ios::in|ios::binary);
    if(!binfile) {
		cerr<<" record.dat open error!"<<endl;
		exit(1);
	}
	rec_size=sizeof(float)+8*sizeof(char); //计算记录大小
	binfile.seekg(0,ios::end);
	rec_num=binfile.tellg()/(rec_size);    //计算记录数
	for(i=0;i<rec_num/2;i++) {
		binfile.seekg((long)i*rec_size,ios::beg);
		binfile.read((char *)name1,8*sizeof(char));   //从前面读记录
        binfile.read((char *)&score1,sizeof(float));
		binfile.seekg(-(long)(i+1)*rec_size,ios::end);   
		binfile.read((char *)name2,8*sizeof(char));   //从后面读记录
        binfile.read((char *)&score2,sizeof(float));    
		binfile.seekp((long)i*rec_size,ios::beg);
		binfile.write((char *)name2,8*sizeof(char));  //将后面的记录写到前面
        binfile.write((char *)&score2,sizeof(float));
		binfile.seekp(-(long)(i+1)*rec_size,ios::end);
		binfile.write((char *)name1,8*sizeof(char));  //将前面的记录写到后面
        binfile.write((char *)&score1,sizeof(float));	
	}
	binfile.seekg(0,ios::beg);
	binfile.read((char *)name1,8*sizeof(char));
    binfile.read((char *)&score1,sizeof(float));
	while(!binfile.eof()) {
		cout<<name1<<'\t'<<score1<<endl;
	    binfile.read((char *)name1,8*sizeof(char));
        binfile.read((char *)&score1,sizeof(float));
	}
	binfile.close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -