首页 > 代码库 > 处理大数据, 编程实现示例

处理大数据, 编程实现示例

#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   84357584
char 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");
}

处理大数据, 编程实现示例