文件读写实验-二

// rw.h

#ifndef rrrrrrrrrrrrrr
#define rrrrrrrrrrrrrr
#include 
#include 
#include 
#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 
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
		 <



你可能感兴趣的:(c/c++)