標籤:
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#include <Windows.h>#include <memory.h>#define path "E:\\雜亂test\\記憶體大資料模型\\1E~001.txt"char **g_pp;int imax = 8435714;//標識有多少行int jmax = 20027;//標識最寬int getJmax(){int width = -1;FILE *pf = fopen(path, "r");//讀檔案開啟路徑if (pf == NULL){printf("檔案開啟失敗");}else{while (!feof(pf)){char readStr[30000] = { 0 };fgets(readStr, 30000, pf);//讀取一行readStr[29999] = ‘\0‘;//最後為字串結束int strLength = strlen(readStr);if (strLength > width){width = strLength;}}fclose(pf);//關閉}return width;}int getImax(){int hang = -1;FILE *pf = fopen(path, "r");//讀檔案開啟路徑if (pf == NULL){hang = -1;printf("檔案開啟失敗");}else{while (!feof(pf)){char readStr[1024] = { 0 };fgets(readStr, 1024, pf);//讀取一行hang++;}fclose(pf);//關閉}return hang;}void loadFromFile(){g_pp = (char **)malloc(sizeof(char*)*imax);//分配指標數組 多少行memset(g_pp, ‘\0‘, sizeof(char*)*imax);FILE *pf = fopen(path, "r");if(pf == NULL){printf("檔案開啟失敗");return;}else{for (int i = 0; i < imax;i++){char str[1024] = { 0 };fgets(str, 1024, pf);//按行讀取str[1023] = ‘\0‘;int strLength = strlen(str);if (strLength < 50){g_pp[i] = malloc(sizeof(char)*(strLength + 1));strcpy(g_pp[i], str);//拷貝到分配的記憶體}}fclose(pf);printf("載入完成\n");}}void search(char *str){if (g_pp != NULL){for (int i = 0; i < imax; i++){if (g_pp[i] != NULL){char *p = strstr(g_pp[i], str);//找到返回地址,否則返回NULLif (p != NULL){puts(g_pp[i]);//列印}}}}}void main(){loadFromFile();while (1){char str[100] = { 0 };scanf("%s", str);search(str);//檢索}system("pause");}
大資料記憶體模型(二級指標)