- 论坛徽章:
- 0
|
为了锻炼自我,同时支持CU论坛,我很费力的做了一题,代码太烂,请随意批评。
- #include <stdio.h>
- #include <string.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #define LENGTH 0x40
- int main()
- {
- FILE *fp;
- char **pptr;
- char chstr[16], buf[LENGTH];
- struct hostent *host_ent;
- fp = fopen("data.txt", "r");
- if (fp == NULL)
- {
- printf("data.txt don't exist in current directory.\n");
- return 0;
- }
- do
- {
- if ( NULL == fgets(buf , LENGTH, fp) )
- break;
- 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;
- }
- pptr = host_ent->h_addr_list;
- int i;
- char iplist[1024] = {0};
- for (i = 0; *pptr != NULL; pptr++)
- {
- inet_ntop(host_ent->h_addrtype, *pptr, chstr, sizeof(chstr));
- strncat(iplist, chstr, strlen(chstr));
- iplist[strlen(iplist)] = 0xa;
- ++i;
- }
- printf("%d\n%s", i, iplist);
- } while (1);
- fclose(fp);
- return 0;
- }
复制代码 Linux 2.6.32, gcc 4.4.5 |
|