从文件里得到所有的邮件地址(源代码)
2003-02-24 13:14
357 查看
很想实现类似 php函数里的 ereg 功能.欢迎探讨
/*
* 从文件里得到邮件地址 2003-2-8 17:15
*/
#include <stdlib.h>
#include <stdio.h>
#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.dat/n",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!='/0';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<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 0-9 之间
while((*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') && *p!='/0')
{
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<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);
// 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);
}
/*
* 从文件里得到邮件地址 2003-2-8 17:15
*/
#include <stdlib.h>
#include <stdio.h>
#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.dat/n",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!='/0';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<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 0-9 之间
while((*p>='0' && *p<='9') || (*p>='a' && *p<='z') || (*p>='A' && *p<='Z') && *p!='/0')
{
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<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);
// 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);
}
相关文章推荐
- 利用正则表达式找出文件里的所有邮件地址
- [Erlang_Question26]怎么通过beam文件得到所有的record或源代码?
- 利用正则表达式找出文件里的所有邮件地址
- php遍历文件夹所有文件,及排除得到的点“.”和“..”的数组
- 得到指定文件夹内所有文件的相关信息
- Android apk反编译图解(得到程序的java源代码,图片、XML配置、语言资源等文件)
- Android APK反编译得到Java源代码和资源文件
- JS 文件得到所有 GET 的值
- Android 5.1 API 22 所有sdk文件下载地址
- JFreeChart的demo.jar。。。教你一步一步反编译并得到所有源代码。。
- 如何得到发送邮件服务器地址(SMTP地址)
- 用bash命令得到Windows一个目录下的所有文件并且把结果输入到一个文件
- PB中得到一个文件地址并取到文件内的数据
- 分析Html源代码,获得所有图片地址
- java邮件系统(java邮件收发系统源代码和下载地址)
- 删除文件夹(非空目录)及其中所有文件的思路及源代码
- 自己写的用JS脚本定时执行Web地址并且得到它的源代码的页面程序
- struts1、struts2所有版本源代码下载地址大全,速度围观
- Java实现获取Web上所有路径下资源文件的URL地址
- sql批量获取wordpress所有留言者的邮件地址