索引与文本文件

时间:2023-02-11 08:40:58

索引与文本文件

1.索引

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//获取多少行,创建数组a[N]
//获取每一行的宽度a[N]=width
//读取的每一个字符串都有\r\n
struct index
{
    int *pindex;
    int length;
}allindex;


int getN()
{
    int i = -1;
    FILE *pf = fopen("test.txt", "rb");//\r\n->\n
    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("\nall=%d", alllength);
        fclose(pf);
        return i;
    }
}
void initindex()
{
    int i = -1;
    FILE *pf = fopen("test.txt", "rb");//\r\n->\n
    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("\ni=%d,index[%d]=%d,with=%d", i, i, allindex.pindex[i], strlen(str));
            i++;

        }
        fclose(pf);
        return i;
    }

}

void main()
{
    allindex.length = getN();
    printf("\nhang=%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("\n索引数组开始分配");
    allindex.length = N;
    allindex.pindex = calloc(N, sizeof(int));//分配内存
    printf("\n索引数组完成分配");

    printf("\n开始读取");
    FILE *pf = fopen(path, "rb");//\r\n->\n
    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("\n结束读取");

    printf("\n开始写入");
    FILE *pfw = fopen(indexpath, "wb");//写入索引
    fwrite(allindex.pindex, sizeof(int), allindex.length, pfw);
    fclose(pfw);//关闭
    printf("\n结束写入");


    free(allindex.pindex);


    printf("\n开始读取");
    FILE *pfr1 = fopen(indexpath, "rb");//写入索引
    fread(allindex.pindex, sizeof(int), allindex.length, pfr1);
    fclose(pfr1);//关闭
    printf("\n结束读取");





}
void qucik()
{
    printf("\n索引数组开始分配");
    allindex.length = N;
    allindex.pindex = calloc(N, sizeof(int));//分配内存
    printf("\n索引数组完成分配");

    printf("\n开始读取");
    FILE *pfw = fopen(indexpath, "rb");//写入索引
    fread(allindex.pindex, sizeof(int), allindex.length, pfw);
    fclose(pfw);//关闭
    printf("\n结束读取");
}


int getN(char *path)
{
    int i = -1;
    FILE *pf = fopen(path, "rb");//\r\n->\n
    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("\n请输入要读取的行数");
        int num = 0;
        scanf("%d", &num);

        fseek(pf, allindex.pindex[num], SEEK_SET);
        char str[128] = { 0 };
        fgets(str, 128, pf);//读取
        printf("\n%s", str);


    }
    fclose(pf);



    system("pause");
}


void main()
{


    FILE *pf1= fopen(indexpath, "rb");
    FILE *pf2 = fopen(path, "rb");
    while (1)
    {
        printf("\n请输入要读取的行数");
        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("\n%s", str);

    }
    fclose(pf1);
    fclose(pf2);


    system("pause");
}