1. 程式人生 > 實用技巧 >【0005】大資料模型_檢索

【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("檔案開啟失敗!\n"); else if (pw == NULL) printf(
"檔案寫入失敗!\n"); 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("請輸入要查詢的資訊:\n"); scanf("%s", str); time_t time_start, time_end; time(&time_start); showlist(str); time(&time_end); printf("查詢花了%.2f秒,與【%s】匹配的記錄數有:%d\n", 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("檔案開啟失敗!\n");
    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("檔案開啟失敗!\n");
    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("檔案寫入失敗!\n");
    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 \n", linenum = getlineNum(path));

    char ** pp = datacache();

    char str[128] = { 0 };
    printf("請輸入要查詢的資訊:\n");
    scanf("%s", str);                         

    while (*str != '0')
    {
        searchrecord(pp, str);
        scanf("%s", str);
    }
    
    system("pause");
}
大資料模型_記憶體檢索