有关EOF.docx

上传人:b****2 文档编号:2256448 上传时间:2022-10-28 格式:DOCX 页数:26 大小:27.87KB
下载 相关 举报
有关EOF.docx_第1页
第1页 / 共26页
有关EOF.docx_第2页
第2页 / 共26页
有关EOF.docx_第3页
第3页 / 共26页
有关EOF.docx_第4页
第4页 / 共26页
有关EOF.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

有关EOF.docx

《有关EOF.docx》由会员分享,可在线阅读,更多相关《有关EOF.docx(26页珍藏版)》请在冰豆网上搜索。

有关EOF.docx

有关EOF

IbelieveIcanmakeamiracle

 

倒着拷贝一个文件到另一个文件

#include

#include

intcopy_from_back(FILE*srcfd,FILE*dstfd);

intmain(intargc,char*argv[])

{

FILE*srcfd=NULL;

FILE*dstfd=NULL;

if(argc!

=3)

{

printf("usage:

FileStudysrcFieldstFile\n");

}

srcfd=fopen(argv[1],"r");

dstfd=fopen(argv[2],"w+");

if(copy_from_back(srcfd,dstfd)==-1)

{

printf("copyerror:

");

exit(EXIT_FAILURE);

}

return0;

}

 

intcopy_from_back(FILE*srcfd,FILE*dstfd)

{

charch;

intnum_read=0;

if(srcfd==NULL||dstfd==NULL)

{

perror("fopenerror:

");

return-1;

}

fseek(dstfd,0,SEEK_SET);

/*指向文件末尾,如果继续读,返回EOF*/

if(fseek(srcfd,1,SEEK_END)!

=0)

{

perror("fseekerror");

}

else

{

printf("begintoreadfile\n");

while(ftell(srcfd)>0)

{

charch=fgetc(srcfd);

if(ch==EOF)

{

printf("fileend!

\n");

fseek(srcfd,-2,SEEK_CUR);

continue;

}

putchar(ch);

fputc(ch,dstfd);

num_read++;

fseek(srcfd,-2,SEEK_CUR);

}

charstart=fgetc(srcfd);

putchar(start);

printf("\nreadfileend:

%d\n",num_read);

}

return0;

}

/*

intcopy_from_back(FILE*srcfd,FILE*dstfd)

{

charch;

if(srcfd==NULL||dstfd==NULL)

{

perror("fopenerror:

");

return-1;

}

fseek(srcfd,0,SEEK_SET);

while((ch=fgetc(srcfd))!

=EOF)

{

putchar(ch);

fputc(ch,dstfd);

fseek(dstfd,1,SEEK_SET);

}

return0;

}

posted@2007-10-1818:

17吴剑阅读(98)|评论(0)|编辑收藏

main函数执行后执行的代码

如果你需要加入一段在main退出后执行的代码,可以使用atexit()函数,注册一个函数。

#include

#include

voidfn1(void),fn2(void),fn3(void),fn4(void);

//atexit()以栈的方式注册函数,先注册的函数会后执行。

voidmain(void)

{

atexit(fn1);

atexit(fn2);

atexit(fn3);

atexit(fn4);

printf(“Thisisexecutedfirst.\n”);

return;

}

voidfn1()

{

printf(“next.\n”);

}

voidfn2()

{

printf(“executed”);

}

voidfn3()

{

printf(“is”);

}

voidfn4()

{

printf(“This”);

}

结果:

Thisisexecutedfirst.

Thisisexecutednext.

 

posted@2007-10-1621:

57吴剑阅读(105)|评论(0)|编辑收藏

另一个sinff抓包程序

linux下用socket的抓包程序

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

voiddie(char*why,intn)

{

perror(why);

exit(n);

}

intdo_promisc(char*nif,intsock)

{

structifreqifr;

strncpy(ifr.ifr_name,nif,strlen(nif)+1);

if((ioctl(sock,SIOCGIFFLAGS,&ifr)==-1))//获得flag

{

die("ioctl",2);

}

ifr.ifr_flags|=IFF_PROMISC;//重置flag标志

if(ioctl(sock,SIOCSIFFLAGS,&ifr)==-1)//改变模式

{

die("ioctl",3);

}

}

//修改网卡成PROMISC(混杂)模式

charbuf[40960];

main()

{

structsockaddr_inaddr;

structether_header*peth;

structiphdr*pip;

structtcphdr*ptcp;

structudphdr*pudp;

charmac[16];

inti,sock,r,len;

char*data;

char*ptemp;

charss[32],dd[32];

if((sock=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1)//建立socket

//mansocket可以看到上面几个宏的意思

{

die("socket",1);

}

do_promisc("eth0",sock);//eth0为网卡名称

system("ifconfig");

for(;;)

{

len=sizeof(addr);

r=recvfrom(sock,(char*)buf,sizeof(buf),0,(structsockaddr*)&addr,&len);

//调试的时候可以增加一个输出r的语句判断是否抓到包

buf[r]=0;

ptemp=buf;

peth=(structether_header*)ptemp;

ptemp+=sizeof(structether_header);//指针后移eth头的长度

pip=(structip*)ptemp;//pip指向ip层的包头

ptemp+=sizeof(structip);//指针后移ip头的长度

switch(pip->protocol)//根据不同协议判断指针类型

{

caseIPPROTO_TCP:

ptcp=(structtcphdr*)ptemp;//ptcp指向tcp头部

printf("TCPpkt:

FORM:

[%s]:

[%d]\n",inet_ntoa(*(structin_addr*)&(pip->saddr)),ntohs(ptcp->source));

printf("TCPpkt:

TO:

[%s]:

[%d]\n",inet_ntoa(*(structin_addr*)&(pip->daddr)),ntohs(ptcp->dest));

break;

caseIPPROTO_UDP:

pudp=(structudphdr*)ptemp;//ptcp指向udp头部

printf("UDPpkt:

\nlen:

%dpayloadlen:

%dfrom%s:

%dto%s:

%d\n",

r,

ntohs(pudp->len),

inet_ntoa(*(structin_addr*)&(pip->saddr)),

ntohs(pudp->source),

inet_ntoa(*(structin_addr*)&(pip->daddr)),

ntohs(pudp->dest)

);

break;

caseIPPROTO_ICMP:

printf("ICMPpkt:

%s\n",inet_ntoa(*(structin_addr*)&(pip->saddr)));

break;

caseIPPROTO_IGMP:

printf("IGMPpkt:

\n");

break;

default:

printf("Unkownpkt,protocl:

%d\n",pip->protocol);

break;

}//endswitch

perror("dump");

}

}

/*

[playmud@fc3test]$gcc-v

Readingspecsfrom/usr/lib/gcc/i386-redhat-linux/3.4.2/specs

Configuredwith:

../configure--prefix=/usr--mandir=/usr/share/man--infodir=/usr/share/info--enable-shar

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1