从文件里得到所有的邮件地址(源代码) (转)

从文件里得到所有的邮件地址(源代码) (转)[@more@]

很想实现类似 PHP函数里的 ereg 功能.欢迎探讨

/*
 * 从文件里得到邮件地址 2003-2-8 17:15
 */
#include
#include
#define MailMaxLen 20
#define bufMaxLen 200

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;
}

int main(int argc,char *argv[])
{
 FILE *fp,*fp_write;
 int i,temp_len;
 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!=2)
 { printf("error!nExp: %s yourmail.datn",argv[0]);
 return 0;
 }
 sprintf(write_file, "tmp_%s", argv[1]); //生成要写入的文件名
 strcpy(fileName, argv[1]);
 if((fp=fopen(fileName,"rb"))==NULL)
 { printf("nCan't open The file for %s to read! n",fileName);
 printf("Press any key to halt!");
 return(0);
 }

 if((fp_write=fopen(write_file,"w"))==NULL)
 { printf("nCan't open The file for %s to write! n",fp_write);
 printf("Press any key to halt!");
 return(0);
 }

 file_len=filesize(fp);
 buf=(char *)malloc(file_len);
 fread(buf,file_len,1, fp);
 for(p=buf;*p!='';p++)
 {
// if(*p=='_' || *p=='.' || (*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') )

 if(*p=='@')
 {
 bool_finished=0;
 bool_find=0;
 mail_begin=p;
 temp_len=0;
 while(temp_len邮箱名
 {
 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 0-9 之间
 while((*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') && *p!='')
 {
 p++;
 //紧接着应该是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 { 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 if(bool_finished==1 && bool_find==0)//不符合
 break;
 }//while
 }// if(mail_begin!=p) //符合条件了
// .NET.cnggg" rel="nofollow">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);
// printf("%c",'n');
 *p=' '; //这样做是为了下次检查时从此开始
 }
 }// if(*p=='@')
// printf("%c",*p);
 }//for
 fclose(fp);
 fclose(fp_write);
 free(buf);
 printf("n--------------------nok!");
 printf("nfile writed to '%s'!",write_file);

 return(0);
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-993458/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-993458/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值