|
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *fp; int i;
char name[256], buf[1024], *ip, *hostname, *etc;
for (;;) {
printf("--- Hostname(. for quit): ");
if (scanf("%255s", name) != 1 || name[0] == '.') break;
fp = fopen("testfile", "r");
if (fp == NULL) return 1;
while (fgets(buf, sizeof buf, fp)) {
ip = strtok(buf, " \t\n");
if (ip == NULL) continue;
hostname = strtok(NULL, " \t\n");
if (hostname == NULL || strcmp(name, hostname)) continue;
printf("IP:%s\nHostname:%s\n", ip, hostname);
for (i = 1; etc = strtok(NULL, " \t\n"); i++)
printf("etc%d:%s\n", i, etc);
}
fclose(fp);
}
return 0;
}
|