test_2012117 whl

1、建立索引

BuildIndex.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define random(x) (rand()%100)

#define NULL 0
#define Att_Order 3    //属性个数
#define Att_Length 10   //属性长度
#define Path_Length 20    //路径长度
struct Index
{
    char attribute[Att_Order][Att_Length];
    char path[Path_Length];
};
void ReadIndex(int start,int end)
{
    FILE *idxfp;
    if ((idxfp = fopen("whl.txt","r")) == NULL)
    {
        printf("Cannot open file");
        exit(0);
    }

    struct Index *context;
    context = (struct Index *)malloc(sizeof(struct Index));

    int j;
    for (j=start; j<=end; j++)
    {
        fseek(idxfp,j*sizeof(struct Index),SEEK_SET);
        fread(context,sizeof(struct Index),1,idxfp);
        int i;
        for (i=0; i<Att_Order; i++)
        {
            printf("%s ",context->attribute[i]);
        }
        printf("%s\n",context->path);
    }
    free(context);
    fclose(idxfp);
}
void WriteIndex(int count)
{
    FILE *idxfp;
    if ((idxfp = fopen("whl.txt","w")) == NULL)
    {
        printf("Cannot open file");
        exit(0);
    }

    struct Index *context;
    context = (struct Index *)malloc(sizeof(struct Index));

    char temp[10];
    int j;
    for (j=0; j<count;j++)
    {
        int k;
        int i;
        for (i=0; i<Att_Order; i++)
        {
            k=random(1000);
            sprintf(temp,"%d",k);
            strcpy(context->attribute[i],temp);
            printf("%s ",context->attribute[i]);
        }
        strcpy(context->path,"c:\\whl");
        printf("%s\n",context->path);

        //fseek(idxfp,0l,SEEK_END);
        fwrite(context,sizeof(struct Index),1,idxfp);
    }
    fclose(idxfp);
}
int main()
{
    int i;
    WriteIndex(100);
    for(i=0; i<100; i++)
    {
        printf("第%d条记录读出:",i+1);
        ReadIndex(i,i);
    }

    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/whl2012/p/2758822.html