• 文本搜索小程序


    一个很简单的小程序程序。使用vs开发,代码只有200行左右。用于在一个路径中搜索某一段文本,包括asc和unicode字符编码。

    工作生活中,经常需要搜索某一段文本,现有工具很少具备这种本文搜索功能。比如everything虽然强大快速,但是只能搜索文件,无法搜索文件内容。为了解决这个问题,花费了一点时间写了一个小程序。

    程序执行时的参数:

    1. 路径
    2. 搜索的字符串。

    源码:

    
    #include 
    #include 
    #include 
    
    using namespace std;
    
    //melody.shop.ele.me/login
    //https://melody.shop.ele.me/login
    
    
    void upcase(char* str, int size,char * dststr) {
    	for (int i = 0; i < size; i++) {
    		if (str[i] >= 'A' && str[i] <= 'Z') {
    			dststr[i] = str[i] + 0x20;
    		}
    		else {
    			dststr[i] = str[i];
    		}
    	}
    }
    
    
    void lowcase(char* str, int size,char * dststr) {
    	for (int i = 0; i < size; i++) {
    		if (str[i] >= 'a' && str[i] <= 'z') {
    			dststr[i] = str[i] - 0x20;
    		}
    		else {
    			dststr[i] = str[i];
    		}
    	}
    }
    
    
    int searchStrInFile(const char * szFileName,const char * pDstContent,const wchar_t *wszcontent)
    {
    	HANDLE hFile = CreateFileA(szFileName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    	if (hFile == INVALID_HANDLE_VALUE)
    	{
    		printf("%s CreateFle:%s error:%d\r\n", __FUNCTION__, szFileName, GetLastError());
    		return FALSE;
    	}
    
    	int asclen = lstrlenA(pDstContent);
    
    	int iSize = GetFileSize(hFile,0);
    	if (iSize < asclen)
    	{
    		CloseHandle(hFile);
    		return FALSE;
    	}
    
    	char * pFileBuf = new char [iSize + 16];
    
    	DWORD dwCnt = 0;
    	int iRes = ReadFile(hFile,pFileBuf,iSize,&dwCnt,0);
    	*(pFileBuf + iSize) = 0;
    	CloseHandle(hFile);
    	if (iRes == 0)
    	{
    		delete [] pFileBuf;
    		return 0;
    	}
    
    	for (int i = 0; i <= iSize - asclen; i ++)
    	{
    		upcase(pFileBuf + i, asclen, pFileBuf + i);
    		if (memcmp(pDstContent, pFileBuf + i, asclen) == 0)
    		{
    			 
    			//delete [] pFileBuf;
    			//return i;
    
    			printf("found string:%s in file:%s,position:%u,continue searching in ther files...\r\n",
    				pDstContent, szFileName, i);
    		}
    	}
    
    
    	int unilen = wcslen(wszcontent) * sizeof(wchar_t);
    	for (int i = 0; i <= iSize - unilen ; i++)
    	{
    		upcase(pFileBuf + i, unilen, pFileBuf + i);
    		if (memcmp((char*)wszcontent, pFileBuf + i, unilen ) == 0)
    		{
    
    			//delete[] pFileBuf;
    			//return i;
    			printf("found string:%s in file:%s,position:%u,continue searching in ther files...\r\n",
    				pDstContent, szFileName, i);
    		}
    	}
    	
    	delete [] pFileBuf;
    	return FALSE;
    }
    
    int searchFile(const char * PreStrPath, int iLayer,const char * pDstContent,const wchar_t * wszcontent)   
    {   
    	int ret = 0;
    
    	static int result = 0;
    
    	int prepathlen = lstrlenA(PreStrPath);
    
    	char strPath[0x1000] = {0};
    	memcpy(strPath,PreStrPath, prepathlen);
    	memcpy(strPath + prepathlen,"\\*.*",lstrlenA("\\*.*"));
    
    
    	WIN32_FIND_DATAA stWfd = { 0 };
    	HANDLE hFind = FindFirstFileA(strPath,&stWfd);
    	if(hFind== INVALID_HANDLE_VALUE)
    	{
    		printf("FindFirstFileA:%s error:%d\r\n",strPath,GetLastError());
    		return 0;  
    	}
    
    	char szLastDir[] = { '.','.',0 };
    	char szCurDir[] = { '.',0 };
    	do
    	{   
    		if (stWfd.dwFileAttributes ==FILE_ATTRIBUTE_DIRECTORY) 
    		{  
    			if (lstrcmpiA(stWfd.cFileName, szLastDir) == 0 || lstrcmpiA(stWfd.cFileName, szCurDir) == 0)
    			{
    				continue;
    			}
    
    			char strNextPath[0x1000] = {0};
    			memcpy(strNextPath, PreStrPath,prepathlen);
    			*(strNextPath + prepathlen) = 0x5c;
    			memcpy(strNextPath + prepathlen + 1,stWfd.cFileName,lstrlenA(stWfd.cFileName));
    			ret = searchFile(strNextPath,iLayer+1,pDstContent,wszcontent);
    			if (ret)
    			{
    				
    			}
    			else {
    
    			}
    		}   
    		else     
    		{   
    			char szFileName[0x1000] = {0};
    			memcpy(szFileName,PreStrPath, prepathlen);
    			*(szFileName + prepathlen) = 0x5c;
    			memcpy(szFileName + prepathlen + 1,stWfd.cFileName ,lstrlenA(stWfd.cFileName));
    			int iFilePos = searchStrInFile(szFileName,pDstContent,wszcontent);
    			if (iFilePos)
    			{
    
    				result++;
    			}
    		}  
    	} while(FindNextFileA(hFind,&stWfd)) ;
    
    	FindClose(hFind);
    
    	return result;
    }
    
    
    
    int main(int argc, char ** argv)
    {
    	int ret = 0;
    
    	wchar_t wszcontent[1024] = { 0 };
    
    	char* searchpath = 0;
    	char* searchstr = 0;
    	if ( argc < 2 )
    	{
    		//printf("parameter wrong!\r\n");
    		//printf("usage 1 (search in defined directory):c:\\test\\ stringvalue\r\n");
    		//printf("usage 2 (search in current directory):stringvalue\r\n");
    		//getchar();
    		//return FALSE;
    
    		printf("input the path the content in which you want to search:");
    		char inputpath[1024];
    		char inputstr[1024];
    		scanf("%s", inputpath);
    
    		printf("input the content you want to search:");
    		scanf("%s", inputstr);
    
    
    		MultiByteToWideChar(CP_ACP, 0, inputstr, -1, wszcontent, sizeof(wszcontent));
    
    		ret = searchFile(inputpath, 1, inputstr, wszcontent);
    	}
    	else {
    		if (argc == 2)
    		{
    			MultiByteToWideChar(CP_ACP, 0, argv[1], -1, wszcontent, sizeof(wszcontent));
    
    			char szcurdir[MAX_PATH];
    			GetCurrentDirectoryA(MAX_PATH, szcurdir);
    
    			printf("start searching string:\"%s\" in path:\"%s\",please to wait patiently...\r\n\r\n", argv[1], szcurdir);
    
    			char szcontent[1024];
    			upcase(argv[1],lstrlenA(argv[1]), szcontent);
    			ret = searchFile(szcurdir, 1, szcontent, wszcontent);
    
    		}
    		else if (argc >= 3)
    		{
    			string path = argv[1];
    			if (path.back() == '/' || path.back() == '\\')
    			{
    				path = path.substr(0, path.length() - 1);
    			}
    			MultiByteToWideChar(CP_ACP, 0, argv[2], -1, wszcontent, sizeof(wszcontent));
    
    			printf("start searching string:\"%s\" in path:\"%s\",please wait patiently...\r\n\r\n", argv[2], path.c_str());
    
    			ret = GetFileAttributesA(path.c_str());
    
    			char szcontent[1024];
    			upcase(argv[2], lstrlenA(argv[1]), szcontent);
    
    			if (ret & FILE_ATTRIBUTE_DIRECTORY)
    			{
    				ret = searchFile(path.c_str(), 1, szcontent, wszcontent);
    			}
    			else {
    				ret = searchStrInFile(path.c_str(), szcontent, wszcontent);
    			}
    		}
    	}
    
    	if (ret == 0)
    	{
    		printf("Search string in path failed!\r\n");
    		getchar();
    		return FALSE;
    	}
    	else
    	{
    		printf("Search string successfully, found string position(offset from beginning):%u\r\n",ret);
    		getchar();
    		return TRUE;
    	}
    	return TRUE;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
  • 相关阅读:
    mysql 数据库使用分享(多图解析)
    【华为OD机试真题 JS】消消乐游戏
    Windows Server 2012 R2 安装 .NET Framework 4.6.1
    VMware Ubuntu16.04 下网络连接不了 修复(unkonwn host)
    ES6异步编程解决方案——async、Generator、Promise
    文本数据分析
    欺诈团伙遇上关联网络,邪不压正
    Element 自定义指令 下拉分页,获取无限数据
    【JavaScript 逆向】极验三代滑块验证码逆向分析
    骨传导耳机哪个牌子好?骨传导好用的品牌推荐
  • 原文地址:https://blog.csdn.net/m0_37567738/article/details/136625486