zoukankan      html  css  js  c++  java
  • jtRecord.c

    #ifdef HAVE_CONFIG_H
    #include<config.h>
    #endif
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<string.h>
    
    #include"HashFile.h"
    #include"jtRecord.h"
    #define KEYOFFSET 0
    #define KEYLEN sizeof(int)
    #define FileNAME "jing.hash"
    
    void showHashFile();
    
    int main(int argc,char *argv[])
    {
    struct jtRecord rec[6] = {
    {1,"jing"},{2,"wang"},{3,"li"},
    {4,"zhang"},{5,"qing"},{6,"yuan"}
    };
    int j=0;
    for(j=0;j<6;j++)
    {
    printf("<%d,%d>	",rec[j].key,hash(KEYOFFSET,KEYLEN,&rec[j],6));
    }
    int fd = hashfile_creat(FileNAME,O_RDWR|O_CREAT,RECORDLEN,6);
    int i=0;
    printf("
    Open and Save Record...
    ");
    fd = hashfile_open(FileNAME,O_RDWR,0);
    for(i=0;i<6;i++)
    {
    hashfile_saverec(fd,KEYOFFSET,KEYLEN,&rec[i]);
    }
    hashfile_close(fd);
    showHashFile();
    printf("
    Find Record...");
    fd = hashfile_open(FileNAME,O_RDWR,0);
    int offset = hashfile_findrec(fd,KEYOFFSET,KEYLEN,&rec[4]);
    printf("
    offset is %d
    ",offset);
    hashfile_close(fd);
    struct jtRecord jt;
    struct CFTag tag;
    fd = open(FileNAME,O_RDWR);
    lseek(fd,offset,SEEK_SET);
    read(fd,&tag,sizeof(struct CFTag));
    printf("Tag is <%d,%d>	",tag.collision,tag.free);
    read(fd,&jt,sizeof(struct jtRecord));
    printf("Record is {%d,%s}
    ",jt.key,jt.other);
    printf("
    Delete Record...");
    fd = hashfile_open(FileNAME,O_RDWR,0);
    hashfile_delrec(fd,KEYOFFSET,KEYLEN,&rec[2]);
    hashfile_close(fd);
    showHashFile();
    fd = hashfile_open(FileNAME,O_RDWR,0);
    char buf[32];
    memcpy(buf,&rec[1],KEYLEN);
    hashfile_read(fd,KEYOFFSET,KEYLEN,buf);
    printf("
    Read Record is {%d,%s}
    ",
    ((struct jtRecord *)buf)->key,((struct jtRecord *)buf)->other);
    hashfile_close(fd);
    printf("
    Write Record...");
    fd = hashfile_open(FileNAME,O_RDWR,0);
    hashfile_write(fd,KEYOFFSET,KEYLEN,&rec[3]);
    hashfile_close(fd);
    showHashFile();
    return 0;
    }
    
    void showHashFile()
    {
    int fd;
    printf("
    ");
    fd = open(FileNAME,O_RDWR);
    lseek(fd,sizeof(struct HashFileHeader),SEEK_SET);
    struct jtRecord jt;
    struct CFTag tag;
    while(1)
    {
    if(read(fd,&tag,sizeof(struct CFTag))<=0)
    break;
    printf("Tag is <%d,%d>	",tag.collision,tag.free);
    if(read(fd,&jt,sizeof(struct jtRecord))<=0)
    break;
    printf("Record is {%d,%s}
    ",jt.key,jt.other);
    }
    close(fd);
    }
    
  • 相关阅读:
    命令行打开无线网络设置
    WebView2 的使用记录
    QT-Mac:在Mac下实现QT程序的打包及公证
    QT:CEF实现js函数与C++函数的异步调用
    QT-Mac:QT Creator 中QT Versions显示无效的QT版本的解决办法
    QT-Mac: Undefined symbols for architecture x86_64
    全局键盘钩子失效与WebRTC的关系分析
    第十二届蓝桥杯决赛 大学 B 组 C/C++ 做题记录
    Luogu P2671 求和 | 数学奇偶性&前缀和
    2021铁三决赛 PWN cardstore | 格式化字符串 & ret2libc
  • 原文地址:https://www.cnblogs.com/1112wlt/p/14159911.html
Copyright © 2011-2022 走看看