zoukankan      html  css  js  c++  java
  • 索引与文本文件

    索引与文本文件

    1.索引

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    //获取多少行,创建数组a[N]
    //获取每一行的宽度a[N]=width
    //读取的每一个字符串都有
    
    struct index
    {
        int *pindex;
        int length;
    }allindex;
    
    
    int getN()
    {
        int i = -1;
        FILE *pf = fopen("test.txt", "rb");//
    ->
    
        if (pf==NULL)
        {
            return -1;
        } 
        else
        {
            int alllength = 0;
            i = 0;
            while (!feof(pf))
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);
                alllength += strlen(str);
                printf("%d  %d   %s ",strlen(str),i, str);
                i++;
            }
            printf("
    all=%d", alllength);
            fclose(pf);
            return i;
        }
    }
    void initindex()
    {
        int i = -1;
        FILE *pf = fopen("test.txt", "rb");//
    ->
    
        if (pf == NULL)
        {
            return -1;
        }
        else
        {
            int alllength = 0;
            i = 0;
    
            while (!feof(pf))
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);
            
                allindex.pindex[i] = alllength;
                alllength += strlen(str);
                
                printf("
    i=%d,index[%d]=%d,with=%d", i, i, allindex.pindex[i], strlen(str));
                i++;
    
            }
            fclose(pf);
            return i;
        }
    
    }
    
    void main()
    {
        allindex.length = getN();
        printf("
    hang=%d", allindex.length);
        allindex.pindex = calloc(allindex.length, sizeof(int));
        initindex();
    
        FILE *pf = fopen("test.txt", "rb");
        while (1)
        {
            int num = 0;
            scanf("%d", &num);
            fseek(pf,allindex.pindex[num] , SEEK_SET);
            char str[128] = { 0 };
            fgets(str, 128, pf);//读取
            printf("%s", str);
    
    
        }
        fclose(pf);
    
        system("pause");
    }

    2.大数据索引

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    
    //读取大数据多少行
    //int a[N] ,堆上
    //写入到文件
    //索引文件载入内存
    //随机读
    char path[256] = "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\1E~001OK.txt";
    char indexpath[256] = "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\1Eindex.txt";
    #define N 84331542
    
    struct index
    {
        int *pindex;//地址
        int length;//长度
    }allindex;
    
    
    void init(char *path)
    {
        printf("
    索引数组开始分配");
        allindex.length = N;
        allindex.pindex = calloc(N, sizeof(int));//分配内存
        printf("
    索引数组完成分配");
    
        printf("
    开始读取");
        FILE *pf = fopen(path, "rb");//
    ->
    
        if (pf == NULL)
        {
            return -1;
        }
        else
        {
            int alllength = 0;
            for (int i = 0; i < N;i++)
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);
                allindex.pindex[i] = alllength;//错位从0开始
    
                int length = strlen(str);
                alllength += length;
                
            }
    
    
    
            fclose(pf);
        }
        printf("
    结束读取");
    
        printf("
    开始写入");
        FILE *pfw = fopen(indexpath, "wb");//写入索引
        fwrite(allindex.pindex, sizeof(int), allindex.length, pfw);
        fclose(pfw);//关闭
        printf("
    结束写入");
    
    
        free(allindex.pindex);
    
    
        printf("
    开始读取");
        FILE *pfr1 = fopen(indexpath, "rb");//写入索引
        fread(allindex.pindex, sizeof(int), allindex.length, pfr1);
        fclose(pfr1);//关闭
        printf("
    结束读取");
    
    
    
    
    
    }
    void qucik()
    {
        printf("
    索引数组开始分配");
        allindex.length = N;
        allindex.pindex = calloc(N, sizeof(int));//分配内存
        printf("
    索引数组完成分配");
    
        printf("
    开始读取");
        FILE *pfw = fopen(indexpath, "rb");//写入索引
        fread(allindex.pindex, sizeof(int), allindex.length, pfw);
        fclose(pfw);//关闭
        printf("
    结束读取");
    }
    
    
    int getN(char *path)
    {
        int i = -1;
        FILE *pf = fopen(path, "rb");//
    ->
    
        if (pf == NULL)
        {
            return -1;
        }
        else
        {
            i = 0;
            while (!feof(pf))
            {
                char str[50] = { 0 };
                fgets(str, 50, pf);
            
            
                i++;
            }
    
            fclose(pf);
            return i;
        }
    }
    
    void main1x()
    {
    
        //printf("%d", getN(path));
        //init(path);
        qucik();
    
        FILE *pf = fopen(path, "rb");
        while (1)
        {
            printf("
    请输入要读取的行数");
            int num = 0;
            scanf("%d", &num);
    
            fseek(pf, allindex.pindex[num], SEEK_SET);
            char str[128] = { 0 };
            fgets(str, 128, pf);//读取
            printf("
    %s", str);
    
    
        }
        fclose(pf);
    
    
    
        system("pause");
    }
    
    
    void main()
    {
    
    
        FILE *pf1= fopen(indexpath, "rb");
        FILE *pf2 = fopen(path, "rb");
        while (1)
        {
            printf("
    请输入要读取的行数");
            int num = 0;
            scanf("%d", &num);
    
            int indexnum = 0;
            fseek(pf1,num*sizeof(int) , SEEK_SET);
            fread(&indexnum, sizeof(int), 1, pf1);//读索引到indexnum
        
            fseek(pf2, indexnum, SEEK_SET);
            char str[128] = { 0 };
            fgets(str, 128, pf2);//读取
            printf("
    %s", str);
    
        }
        fclose(pf1);
        fclose(pf2);
    
    
        system("pause");
    }
  • 相关阅读:
    verilog之计数器0~9999——数码管显示
    C语言的谜题
    modelsim SE6.2b的常用问题
    [转载]MDK常见报错(编译arm)
    Nios II之LED实验(SDRAM+EPCS4配置)————基于Altera的DE0开发板
    poj2210
    poj1161
    poj1555
    poj2337
    poj1107
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5880292.html
Copyright © 2011-2022 走看看