文件读写实验-二

// rw.h

#ifndef rrrrrrrrrrrrrr
#define rrrrrrrrrrrrrr
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define  version 1.0.0

#define   OFFSET16bit(x) ( (x + 15) >> 4 << 4 ) 

class OBJ{
public:
	char		ID[32];
	char		score[8];
	char		name[32];	
	char		HomeAddress[128];
};


void WriteOBJ(FILE* pf, OBJ object);
void ReadObj(FILE* pf, OBJ &obj, int idx);
#endif


// read.cpp

#include "rw.h"

void  ReadObj(FILE* pf, OBJ &obj, int idx)
{
	fflush(pf);
	fseek(pf, 0, SEEK_END);
	int sz = ftell(pf);

	const int HeadSize = 100;
	int objsz = sizeof(OBJ);

	int cnt = 0;

	/*
	class OBJ{
	public:
	char		ID[32];
	char		score[8];
	char		name[32];	
	char		HomeAddress[128];
	};
	*/
	long objoffset = HeadSize + idx*objsz;  
	fseek(pf, objoffset , SEEK_SET);
	int idsz = 0, scoresz = 0, namesz = 0, hasz = 0;
	fread(&idsz, sizeof(int), 1, pf);
	fread(&scoresz, sizeof(int), 1, pf);
	fread(&namesz, sizeof(int), 1, pf);
	fread(&hasz, sizeof(int), 1, pf);

	objoffset += 4 * sizeof(int);

	fread(obj.ID, idsz, 1, pf);

	objoffset += sizeof(obj.ID);
	
	fseek(pf, objoffset, SEEK_SET);

	fread(obj.score, scoresz, 1, pf);

	objoffset += sizeof(obj.score);

	fseek(pf, objoffset, SEEK_SET);

	fread(obj.name, namesz, 1, pf);

	objoffset += sizeof(obj.name);

	fseek(pf, objoffset, SEEK_SET);

	fread(obj.HomeAddress, hasz, 1, pf);

	fflush(pf);

}

// write.cpp

#include "rw.h"


void WriteOBJ(FILE* pf, OBJ object)
{
	fflush(pf);
	fseek(pf, 0, SEEK_END);
	int sz = ftell(pf);
	const int HeadSize = 100;
	int objsz = sizeof(OBJ);
	int cnt = 0;
	if (sz == 0)
	{
		fseek(pf, 0, SEEK_SET);	
		printf("new file create\n");
		char VersionInfo[64] = "JSSJ Indexing Module Version 1.0.0";
		int len = strlen(VersionInfo) + 1;
		fwrite(&len, sizeof(len), 1, pf);
		fwrite(VersionInfo, strlen(VersionInfo), 1, pf);
		fwrite("\0", 1, 1, pf);
		fseek(pf, 80, SEEK_SET);
		fwrite(&cnt, sizeof(cnt), 1, pf);
		fflush(pf);
	}

	fseek(pf, 80, SEEK_SET);
	fread(&cnt, sizeof(cnt), 1, pf);
	
	fflush(pf);
	/*
	class OBJ{
	public:
	char		ID[32];
	char		score[8];
	char		name[32];	
	char		HomeAddress[128];
	};
	*/

	long curoffset = HeadSize + cnt * objsz;
	fseek(pf, curoffset, SEEK_SET);
	int IDlen = strlen(object.ID) + 1;
	int scoreLen = strlen(object.score) + 1;
	int nameLen = strlen(object.name) + 1;
	int homeAdressLen = strlen(object.HomeAddress) + 1;
	fwrite(&IDlen, sizeof(int), 1, pf);
	fwrite(&scoreLen, sizeof(int), 1, pf);
	fwrite(&nameLen, sizeof(int), 1, pf);
	fwrite(&homeAdressLen, sizeof(int), 1, pf);

	fwrite(object.ID, strlen(object.ID), 1, pf);
	fwrite("\0", 1, 1, pf);

	long of = curoffset + 4 * sizeof(int) + sizeof(object.ID);

	fseek(pf, of, SEEK_SET);
	fwrite(object.score, strlen(object.score), 1, pf);
	fwrite("\0", 1, 1, pf);

	of = of + sizeof(object.score);

	fseek(pf, of, SEEK_SET);
	fwrite(object.name, strlen(object.name), 1, pf);
	fwrite("\0", 1, 1, pf);

	of = of + sizeof(object.name);

	fseek(pf, of, SEEK_SET);
	fwrite(object.HomeAddress, strlen(object.HomeAddress), 1, pf);
	fwrite("\0", 1, 1, pf);

	cnt++;
	
	fseek(pf, 80, SEEK_SET);
	fwrite(&cnt, sizeof(cnt), 1, pf);

	fflush(pf);

}

// test.cpp

#include "rw.h"
#include <iostream>
using namespace std;

int main()
{
	FILE *pf;
	pf = fopen("./Index.dat", "w+");
	OBJ obj1, obj2, obj3;

	strcpy(obj1.ID,"sy0915120");
	strcpy(obj1.score, "80");
	strcpy(obj1.name, "tangzesheng");
	strcpy(obj1.HomeAddress, "JiangxiJiujiang");

	strcpy(obj2.ID, "sy0915125");
	strcpy(obj2.score, "90");
	strcpy(obj2.name, "xushenghui");
	strcpy(obj2.HomeAddress, "HubeiXiantao");

	strcpy(obj3.ID, "sy0915214");
	strcpy(obj3.score, "79");
	strcpy(obj3.name, "wangshuang");
	strcpy(obj3.HomeAddress, "henan");

	WriteOBJ( pf, obj1);
	WriteOBJ( pf, obj2);
	WriteOBJ( pf, obj3);
	OBJ obj4;

	ReadObj(pf, obj4, 1);

	cout << "ID: "
		 << obj4.ID
		 << endl
		 << "Score: "
		 << obj4.score
		 <<endl
		 << "Name: "
		 << obj4.name
		 << endl
		 << "Address: "
		 << obj4.HomeAddress
		 << endl;

	fclose(pf);

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值