【0005】大数据模型_检索

硬盘检索

将硬盘中的文件逐行读入内存进行检索,速度慢

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

char path[256] = "C:\Users\Administrator\Desktop\downloads\data.txt";
char savepath[512] = { 0 };
int count = 0;            // 符合条件的记录数

void showlist(char *str)
{
    sprintf(savepath, "C:\Users\Administrator\Desktop\downloads\%s.txt", str);        // 检索结果文件
    char linestr[1024] = { 0 };
    FILE *pw = fopen(savepath, "w");

    FILE *pf = fopen(path, "r");
    if (pf == NULL)
        printf("文件打开失败!
");
    else if (pw == NULL)
        printf("文件写入失败!
");
    else
    {
        while (!feof(pf))
        {
            fgets(linestr, 1024, pf);
            if (strstr(linestr, str) != NULL)
            {
                puts(linestr);
                fputs(linestr, pw);
                count++;
            }
        }
    }

    fclose(pw);
    fclose(pf);
}

void main010()
{
    char str[128] = { 0 };
    printf("请输入要查询的信息:
");
    scanf("%s", str);

    time_t time_start, time_end;
    
    time(&time_start);

    showlist(str);

    time(&time_end);

    printf("查询花了%.2f秒,与【%s】匹配的记录数有:%d
", difftime(time_end, time_start), str, count);
    system(savepath);        // 打开新生成的匹配信息的文件

    system("pause");
}
大数据模型_硬盘检索

内存检索

将硬盘中的文件全部读入内存进行检索,速度快

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char path[256] = "C:\Users\Administrator\Desktop\downloads\data.txt";
static char savepath[256] = { 0 };

int linenum = 0;        // 文件的行数

// 计算待检索文件的行数
int getlineNum(char * path)
{
    FILE *pf = fopen(path, "r");
    if (pf == NULL)
        printf("文件打开失败!
");
    else
    {
        char strline[1024] = { 0 };

        while (!feof(pf))
        {
            fgets(strline, 1024, pf);
            linenum++;
        }
    }

    fclose(pf);
    return linenum;
}

// 将数据载入内存
char ** datacache()
{
    char ** g_pp = NULL;

    FILE *pf = fopen(path, "r");
    if (pf == NULL)
        printf("文件打开失败!
");
    else
    {
        char strline[30000] = { 0 };

        g_pp = (char **)calloc(linenum, sizeof(char **));

        for (int i = 0; i < linenum; i++)
        {
            fgets(strline, 30000, pf);
            g_pp[i] = (char *)calloc(strlen(strline), sizeof(char *));
            strcpy(g_pp[i], strline);
        }
    }

    fclose(pf);
    return g_pp;
}

// 从内存中检索信息
void searchrecord(char **g_pp, char * str)
{
    sprintf(savepath, "C:\Users\Administrator\Desktop\downloads\%s.txt", str);
    FILE *pw = fopen(savepath, "w");

    if (pw == NULL)
        printf("文件写入失败!
");
    else
    {
        for (int i = 0; i < linenum; i++)
        {
            if (strstr(g_pp[i], str) != NULL)
            {
                fputs(g_pp[i], pw);
            }
        }
    }
    fclose(pw);
    system(savepath);
}

void main()
{
    printf("%d 
", linenum = getlineNum(path));

    char ** pp = datacache();

    char str[128] = { 0 };
    printf("请输入要查询的信息:
");
    scanf("%s", str);                         

    while (*str != '0')
    {
        searchrecord(pp, str);
        scanf("%s", str);
    }
    
    system("pause");
}
大数据模型_内存检索
原文地址:https://www.cnblogs.com/ant-colonies/p/13419834.html