標籤:
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include<string.h>#include <time.h>#include <process.h>//多線程char g_path[200] = "I:\\BaiduYunDownload\\1億QQ信封\\1E5-10位\\QQ信封.txt";#define N 84357584char str[100] = { 0 };char **g_pall = NULL;//二級指標儲存一級指標數組的地址int getN(){FILE *pfr = fopen(g_path, "r");if (pfr == NULL){return -1;}else{int i = 0;while (!feof(pfr)){char strbuffer[256] = { 0 };fgets(strbuffer, 256, pfr);//讀取一行i++;}fclose(pfr);//關閉檔案return i;}}void eatN(char *str){while (*str != '\0'){if (*str == '\n'){*str = '\0';}str++;}}void init(){g_pall = (char **)malloc(sizeof(char *)*N);//動態分配一個指標數組memset(g_pall, '\0', sizeof(char *)*N);FILE *pfr = fopen(g_path, "r");if (pfr == NULL){printf("初始化失敗");return;}else{for (int i = 0; i < N ; i++){char strbuffer[79] = { 0 };fgets(strbuffer, 79, pfr);//讀取一行//printf("%s,%d", strbuffer,strlen(strbuffer));eatN(strbuffer);//printf("%s,%d", strbuffer, strlen(strbuffer));int length = strlen(strbuffer);//求擷取長度if (length != 0){g_pall[i] = malloc(sizeof(char)*(length + 1));//每個指標分配記憶體if (g_pall[i] == NULL){printf("\nfail");}strcpy(g_pall[i], strbuffer);//拷貝字串//printf("\n%s", g_pall[i]);//列印結果}}fclose(pfr);//關閉檔案}}void search(char *str){int j = 0;for (int i = 0; i < N-1; i++){if (g_pall[i] !=NULL){char *p = strstr(g_pall[i], str);//查詢if (p != NULL){j++;printf("\n%s", g_pall[i]);//列印結果}}}printf("\nj=%d", j);//列印速度慢於查詢}void main(){//printf("%d", getN());init();printf("已經載入完成");while (1){//time_t start, end;time(&start);//int i = 0;//scanf("%d", &i);//printf("\n%s", g_pall[i]);scanf("%s", str);search(str);time(&end);printf("\n一共%d S", (unsigned int)(end - start));}system("pause");}
處理大資料, 編程實現樣本