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

📄 main.cpp

📁 从文件中搜索EMAIL地址
💻 CPP
字号:



/*判断一个字符串是否一个EMAIL地址
 *是就返回1,否就返回0
 */
/*
int IsEmailAddr(char *mail)
{

}
*/

/*
#include   <stdio.h>   
#include   <math.h>   
//#include   <string.h>   
#include   <stdlib.h>   

int   strLength(char   *);   
int*   charcount(char   *,int,char);   
int   isEmail(char   *,int);   

void   main(){   
	int   len;   
	char   str[20];   
	printf("请输入一行字符串:");   
	scanf("%s",str);   
	len=strLength(str);   
	if(isEmail(str,len)   &&   len   <=19)   
		printf("您输入的地址合法!\n");   
	else   
		printf("您输入的地址不合法!\n");   
}   

int   strLength(char   *p){   
	int   count=0;   
	for(;*p!='\0';p++){   
		count++;   
	}   
	return   count;   
}   

 
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int* charcount(char *p, int n, char ch)
{   
	int *ptr, i;   
	ptr = (int *)malloc(sizeof(int)*2);   
	*ptr = *(ptr+1) = 0;   
	for(i = 0; *(p+i) != '\0'; i++)
	{   
		if(ch == *(p+i))
		{   
			*ptr++;   
			if(*ptr == 1)
			{   
				*(ptr+1) = i;   
			}   
		}   
	}   
	return ptr;   
}  

int isEmail(char *p, int n) 
{   
	if(*p!='@' && *p!='.' && *(p+n-1)!='@' && *(p+n-1)!='.'   
		&& *charcount(p,n,'@')==1 && *charcount(p,n,'.')>=1   
		&& *(charcount(p,n,'@')+1) < *(charcount(p,n,'.')+1)   
		&& abs(*(charcount(p,n,'@')+1) - *(charcount(p,n,'.')+1)) !=1 )
	{   
		return 1;   
	}   
	else
	{   
		return 0;   
	}   
}   

int main(int argc, char **argv)
{
	char mail[64];
	printf("please input the mail address: and the \"q\" is exit\n");
	while (gets(mail) != NULL)
	{
		if (strcmp("q", mail) == 0)
			exit(0);
		if (isEmail(mail, strlen(mail)) == 1)
		{
			printf("the address is a valid:\n");
		}
		else
		{
			printf("the address is a invalid:\n");
		}
		printf("please input the mail address: and the \"q\" is exit\n");
	}
	return 0;
}
*/

/*
 * 从TXT文件里得到邮件地址 2007-9-13 17:20
 */
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h>

#define mailMaxLen 30
#define bufMaxLen 200
#define MAX_FILE_LENGTH 128
#define DEF_LINE_BUFF_SIZE 128

long filesize(FILE *stream) 
{ 
	long curpos, length; 
	curpos = ftell(stream); 
	fseek(stream, 0L, SEEK_END); 
	length = ftell(stream); 
	fseek(stream, curpos, SEEK_SET); 
	return length; 
}

void Classify50(int line, char *srcfile, char *desfile, int desNum);
int main(int argc,char *argv[])
{
	FILE *fp, *fp_write;
//	int i;
	int temp_len;
	int total = 0;
	int bool_find=0, bool_finished=0;
	long file_len=0;
	char fileName[30], write_file[30];
//	char mail_data[50];     //保存得到的临时邮件
	char *buf;
	char *p, *mail_begin, *temp_p;
	if(argc != 3)
	{
		printf("usage %s yourmail.txt writemail.txt\n",argv[0]);
		return 0; 
	}
	sprintf(write_file, "%s", argv[2]);  //生成要写入的文件名
	strcpy(fileName, argv[1]);
	if((fp = fopen(fileName, "rb")) == NULL)
	{ 
		printf("\nCan't open The file for %s to read! \n",fileName); 
		exit(1);
	}
	
	if((fp_write = fopen(write_file, "w+")) == NULL)
	{ 
		printf("\nCan't open The file for %s to write! \n",fp_write); 
		exit(1);		
	}
	
	
	file_len = filesize(fp);
	buf= (char *)malloc(file_len);
	fread(buf, file_len, 1, fp);
//	FILE *fp_test;
	//to test
	/*------------------------------------------
	fp_test = fopen("test_tmp1.txt", "w+");
	fwrite(buf, file_len, 1, fp_test);
	fclose(fp_test);
	------------------------------------------*/
	for (p=buf; *p != '\0'; p++)
	{
		//  if(*p=='_' || *p=='.' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') )
		//to test
		/*------------------------------------------
		fp_test = fopen("test_tmp2.txt", "w+");
		fwrite(p, file_len, 1, fp_test);
		fclose(fp_test);
		------------------------------------------*/
		if (*p == '@')
		{
			bool_finished = 0;
			bool_find = 0;
			mail_begin = p;
			//to test
			/*------------------------------------------
			fp_test = fopen("test_tmp3.txt", "w+");
			fwrite(mail_begin, file_len, 1, fp_test);
			fclose(fp_test);
			------------------------------------------*/
			temp_len = 0;
			while (temp_len < mailMaxLen && p != buf)	//后退,找到邮箱名
			{
				p--;
				//如果符合标准的
				if(*p=='_' || *p=='-' ||  *p=='.' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
				{
					temp_len++;
				}
				else
				{
					break;
				}
			}
			p++;//指向正确的开始
			//eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$email)) 
			if(mail_begin != p)   //符合条件了
			{
				temp_p = mail_begin;  //这时temp_p 指向@处
				mail_begin = p;   //记录邮件名开始地址
				p = ++temp_p;    //p指向@后的第一个字符
				//    printf("%s %c","ok",*temp_p);
				
				//接下来的第1个字符应该是a-z A-Z 0-9 之间
				while((*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') && *p!='\0')
				{
					p++;
					//紧接着应该是a-z - 0-9 A-Z
					if(*p=='-' || (*p>='0' && *p<='9')  || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
					{ 
						p++;
					}
					else
					{ 
						bool_find=0;
						break;
					}
					temp_len=0;   //cd.digital@cfan.com.cn
					while(temp_len < mailMaxLen && *p!='\0')
					{ 
						if(*p=='-' || (*p>='0' && *p<='9')  || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))
						{ 
							p++;
							temp_len++;
						}//
						else if(*p == '.')//找到点.了  +[a-z]{2,3}$
						{ 
							bool_find++;//找到一段了
							temp_p=p;  //temp_p 指向.所在的位置
							p++;
							break;
						}
						else
						{ 
							bool_finished=1; //退出循环
							break;
						}
					}//while(temp_len<mailMaxLen && *p!='\0')
					if (bool_finished==1 && bool_find==0)//不符合
						break;
				}//while
			}//   if(mail_begin!=p)   //符合条件了
			// ggg@chinatoolsnet.cnggg _w@163.net
			if(bool_find > 0)
			{
				p = temp_p;  //p退到最后一个.的位置
				p = p+3;
				if (!((*p>='a' && *p<='z') || (*p>='A' && *p<='Z'))) //最后的 .net 不符合
				{ 
					p--;
				}
				/*
				for(temp_p=mail_begin;temp_p<=p;temp_p++)
				{ printf("%c",*temp_p);
				}
				*/
				//   strncpy(mail_data, mail_begin, 3); 
				fwrite(mail_begin, p-mail_begin+1, 1, fp_write);
				fputc('\n',fp_write);
				total++;
				//    printf("%c",'\n');
				*p = ' '; //这样做是为了下次检查时从此开始
			}
		}//  if(*p=='@')
		//  printf("%c",*p);
	}//for 
	fclose(fp);
	fclose(fp_write);
	free(buf);
	printf("\nsearched %d lines\nok!", total);
	printf("\nfile writed to '%s'!",write_file);

	Classify50(total, write_file, "yyzs", 0);
	
	return(0);
}


void Classify50(int line, char *srcfile, char *desfile, int desNum)
{
	FILE *src;
	FILE *des;
	int iCount = 1;	//总行数计数器
	int fileNumber = (line % 50 == 0) ? (line/50) : (line/50 + 1);		//生成的文件数
	char szBuffFile[MAX_FILE_LENGTH];	//文件名
	char szLine[DEF_LINE_BUFF_SIZE];		
	memset(szBuffFile, 0, sizeof(szBuffFile));
	if ((src = fopen(srcfile, "r")) == NULL)
	{
		printf("open %s error\n", srcfile);
		exit(1);
	}
	
	for (int i=1+desNum; i <= fileNumber+desNum; i++)
	{
		sprintf(szBuffFile, "%s(%d)工作表.xls", desfile, i);
		if ((des = fopen(szBuffFile, "w+")) == NULL)
		{
			printf("file open error\r\n");
			exit(1);
		}
		int j = 1;
		while (j++ <= 50 && iCount++ <= line)
		{
			memset(szLine, 0, sizeof(szLine));
			fgets(szLine, sizeof(szLine), src);
			fputs(szLine, des);
		}
		fclose(des);
	}
	fclose(src);
}

⌨️ 快捷键说明

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