⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findkey.cpp

📁 小型搜索引擎,用C/C++编写,属于全文搜索引擎
💻 CPP
字号:
#include "Tse.h"#include "Url.h"using namespace std;extern int FindUrl(const char* url, char **content);/* * Find results pages containing query words. * Return the number of results. */int FindKey(const char* key){	FILE *fpfile;	int flag=2,fpos,c,count=0;	char fbuffer[URL_LEN],buffer[URL_LEN],url[URL_LEN];	fpfile = fopen(DATA_FILE_NAME.c_str(),"r");	if(fpfile == NULL){		return -1;	}			while( (c=fgetc(fpfile)) != EOF ){		int b;		if(c=='<'){			flag=1;		}		if(c=='>'){			flag=2;		}		// for Chinese words		b=key[0]<0 ? 256+key[0]: key[0];		if(b!=c) continue;		fpos = ftell(fpfile);		memset(fbuffer, 0, URL_LEN);		fread(fbuffer,1,strlen(key)-1,fpfile);		if( memcmp(key+1,fbuffer,strlen(key)-1) !=0 ){			fseek(fpfile,fpos,SEEK_SET);			continue;		}		if(flag==1) continue;		// <,in <>		fseek(fpfile,-25,SEEK_CUR);		memset(buffer, 0, URL_LEN);		fread(buffer,1,50,fpfile);		fseek(fpfile,-25,SEEK_CUR);		//make sure not to meet the end of file here		for(;;){			if(feof(fpfile)) return -1;			if(fgetc(fpfile)!=1) continue;			if(feof(fpfile)) return -1;			if(fgetc(fpfile)!=1) continue;			if(feof(fpfile)) return -1;			if(fgetc(fpfile)!=1) continue;			if(feof(fpfile)) return -1;			if(fgetc(fpfile)!=91) continue;			int i=0;			int charVal;			char* charPos;			memset(url, 0, URL_LEN);			while( (charVal = fgetc(fpfile)) ){				if(charVal==93) break;				url[i]=charVal;				i++;			}			url[i]=0;			printf("###[%s] contains the key.\n",url);			printf("......");			i=0;			while(buffer[i]==0) i++;			charPos = buffer+i;			charPos[49] = '\0';			printf("%s",charPos);			printf("......\n");			count++;			break;		}		cout << "What to do next?(c:continue/q:quit/s:"			<< "show the correspanding web page)." << endl;		for(;;){			string c;			for(;;){				cin >> c;				if( c == "c" || c == "q" || c == "s" ) break;			}						if( c == "c" ) break;			if( c == "q" ) {				fclose(fpfile);				return count;			}			if( c == "s" ){				char *sContentBuf=NULL;				if( FindUrl(url, &sContentBuf) == -1 ){					printf("Haven't found the url.\n");				}else{					for(unsigned int i=512; (i<strlen(sContentBuf))&&(i<600); i++){						if((sContentBuf[i] == '\n') || (sContentBuf[i] == '\r')){							sContentBuf[i] = '\0';							break;						}						if(i == 599){							sContentBuf[i] = '\0';						}					}					cout << endl << sContentBuf << endl << "......" << endl << endl;					if( sContentBuf) free(sContentBuf);				}				cout << "What to do next?(c:continue/q:quit/s:"					<< "show the correspanding web page)." << endl;			}			}	}	fclose(fpfile);	return count;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -