多线程文本查询

多线程查询

 前面c语言-文件操作(3)-文件分割与合并 ,将大文件切割个若干份小文件,于是才有这篇笔记.主要思想是通过每个线程读取一个文件,并进行查找.

代码

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


struct FileInfo
{
	char *path;									//文件路径
	char *findStr;									//要查找的字符串
	int id;
};
struct FileInfo *pFileInfo;


void searchTxt(void *p)									//根据字符串去文本中查找	
{
	struct FileInfo *pInfo = p;
	if (p == NULL)
	{
		printf("文件信息不能为空!\n");
		return;
	}
	FILE *pReader = fopen(pInfo->path, "rb");
	if (pReader == NULL)
	{
		printf("无法打开该%s文件!\n", pInfo->path);
		return;
	}
	while (!feof(pReader))
	{
		char tmpStr[512] = { 0 };
		fgets(tmpStr, 512, pReader);
		if (tmpStr != NULL)
		{
			char *pTmp = strstr(tmpStr, pInfo->findStr);
			if (pTmp != NULL)
			{
				printf("线程id=%d 找到:%s", pInfo->id, tmpStr);
			}
		}
	}
	fclose(pReader);
}

void splitSegment(char *path, char *str)						//将任务进行分块,每个线程负责一个文件
{
	int threadCount = 8;								//确定线程数量
	HANDLE *hd = calloc(threadCount, sizeof(HANDLE));			        //根据线程数量分配内存

	pFileInfo = calloc(threadCount, sizeof(struct FileInfo));

	char pathInfo[64] = "G:\\bigdata\\kf\\1_1.txt";
	int pathLength = strlen(pathInfo) + 1;

	int strLen = strlen(str) + 1;

	for (int i = 0; i < threadCount; i++)
	{
		pFileInfo[i].path = calloc(pathLength, sizeof(char));
		sprintf(pFileInfo[i].path, "G:\\bigdata\\kf\\1_%d.txt", (i + 1));

		pFileInfo[i].id = i;

		pFileInfo[i].findStr = calloc(strLen, sizeof(char));
		strcpy(pFileInfo[i].findStr, str);

		hd[i] = _beginthread(searchTxt, 0, &pFileInfo[i]);		//启动线程调用
	}

	WaitForMultipleObjects(threadCount, hd, TRUE, INFINITE);	       //等待所有线程执行完毕

}

int main(int argc, char *argv[])
{
	char filePath[64] = "G:\\bigdata\\kf\\1_1.txt";

	while (1)
	{
		printf("请输入要查找的字符串:");
		char str[128] = { 0 };
		scanf("%s", str);
		splitSegment(filePath, str);
	}
	system("pause");
	return 0;
}


秋风 2016-07-14