- 论坛徽章:
- 0
|
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#define LENGTH 0x40
int main()
{
FILE *fp,*fp2;
char ** ptr;
char chstr[16],buf[LENGTH];
struct hostent * host_ent;
fp=fopen("data.txt","r");
fp2=fopen("output.txt","wb");
if(NULL==fp)
{
printf("data.txt don't exist in the directory.\n");
return 0;
}
if(NULL==fp2)
{
printf("output.txt don't exist in the directory.\n");
return 0;
}
while(fgets(buf,LENGTH,fp))
{
if(buf[0]=='\n'||buf[0]=='\n'||buf[0]=='\r'||buf[0]=='\t')
continue;
fputs(buf,fp2);//保存到特定文件
printf("%s",buf);
buf[strlen(buf)-1]=0x0;
if(!(host_ent=gethostbyname(buf)))
{
printf("error occur when querying the ip\n");
fclose(fp);
return -1;
}
ptr=host_ent->h_addr_list;
int i;
char iplist[1024]={0};
for(i=0;*ptr!=NULL;ptr++)
{
inet_ntop(host_ent->h_addrtype,*ptr,chstr,sizeof(chstr));
strncat(iplist,chstr,strlen(chstr));
iplist[strlen(iplist)]=0xa;
++i;
}
printf("%d\n%s",i,iplist);
fputs(iplist,fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
//对3楼的代码进行修改,将结果保存到output.txt中,对空格进行了处理 |
|