69,371
社区成员
发帖
与我相关
我的任务
分享
//
// 随手写的,没有检查、测试,程序中也没有检查参数及返回值,仅供参考
//
// 索引项
struct IndexItem{
long offset; // 记录位置
unsigned long hashkey; // 为便于排序/查找,避免频繁访问文件,可以对记录的键值进行散列
};
// 由于文件较大,可以一次读取、排序若干条记录,形成索引块,然后进行合并
// 若干索引块可以用链表组织起来
struct IndexBlock{
IndexItem * index;
int itemcount;
IndexBlock * nextblock;
};
// 为一条记录生成散列值
unsigned long hashKeyValue(const char * item){
// 根据需要设计一个散列算法
return(((unsigned long *)(const void*)item)[0] ^ ((unsigned long *)(const void*)item)[1]);
}
// 比较两条记录
int compRecord(const void * item1,const void * item2){
// 先比较 hashkey,如果相同再读取文件进行准确的比较
//..........
}
// 一次读取idxCount条记录
int readDataBlock(FILE* inputfile, IndexItem * idx,int idxcount){
int i;
char buffer[BUFFERSIZE];
for(i = 0; i < idxcount && !feof(inputfile);i++){
fgets(buffer,BUFFERSIZE,inputfile);
idx[i].offset = ftell(inputfile);
idx[i].hashkey = hashKeyValue(buffer);
}
return (i);
}
// 读取文件记录
int readData(FILE * inputfile, IndexBlock * head){
int count = 0;
IndexBlock * newblock = head;
do{
newblock->nextblock = new IndexBlock;
newblock = newblock->nextblock;
newblock->index = new IndexItem[BLOCKSIZE];
newblock->itemcount = readDataBlock(inputfile,newblock->index,BLOCKSIZE);
count += newblock->itemcount;
}while(!feof(inputfile));
newblock->nextblock = NULL;
return (count);
}
// 合并两个索引块
int unionIndexBlock(const IndexBlock * index1,const IndexBlock * index2,IndexBlock * resuindex){
int i1,i2,ir,total;
total = index1->itemcount + index2->itemcount;
for(ir = i1 = i2 = 0; ir < total; ir++){
if(compRecord(index1->index[i1],index2->index[i2]) > 0){
copyIndex(index2->index[i2],resuindex->index[ir]);
i2++;
}else{
copyIndex(index1->index[i1],resuindex->index[ir]);
i1++;
}
}
resuindex-itemcount = ir;
return (resuindex->itemcount);
}
// 读取文件并创建索引
IndexBlock * createIndex(FILE * inputfile){
IndexBlock head;
IndexBlock * currblock;
IndexBlock * newblock;
readData(inputfile, &head);
// 排序
for(currblock = head.nextblock; currblock != NULL; currblock = currblock->nextblock){
qsort((void*)currblock->head,currblock->itemcount,sizeof(IndexItem),compRecord);
}
// 合并索引块
while(head.nextblock->nextblock != NULL){
currblock = &head;
while(true){
if(currblock->nextblock == NULL || currblock->nextblock->nextblock == NULL)break;
newblock = new IndexBlock;
newblock->index = new IndexItem[currblock->nextblock->itemcount + currblock->nextblock->nextblock->itemcount];
newblock->nextblock = currblock->nextblock->nextblock->nextblock;
unionIndexBlock(currblock->nextblock,currblock->nextblock->nextblock,newblock);
delete []currblock->nextblock->nextblock->index;
delete []currblock->nextblock->index;
delete currblock->nextblock->nextblock;
delete currblock->nextblock;
currblock->nextblock = newblock;
currblock = newblock;
}
}
return (head.nextblock);
}
#define MAXSIZE 1000
#define BUFFSIZE 1000
fpos_t index[MAXSIZE];
char buffer[BUFFSIZE];
for(int i = 0;i < MAXSIZE;i++){
fgetpos(index + i,inputFile);
fgets(buffer,BUFFSIZE,inputFile);
if(feof(inputFile))break;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 256
int main()
{
FILE *F = fopen("file.txt","rt");
char buff[M];
char searchstr[M];
char *find = NULL;
if (F == NULL) exit(1);
printf("输入你要找的纪录号:");
scanf("%s",searchstr);
while (!feof(F))
{
fgets(buff,M-1,F);
if (buff[0] == '\n') continue; /*如果是空行直接再读取下一行*/
find = strstr(buff,searchstr);
if (find != NULL)
{
puts(buff); find = NULL;
}
}
fclose(F);
system("PAUSE");
return 0;
}
输入你要找的纪录号:2008_54
2008_54,25,文件记录文件记录,文件记录文件记录.aaa
请按任意键继续. . .