I've made this program that converts hostnames from a file (hosts.txt)
to ip addresses which are stored in a file called ipaddresses.txt.

But the problem is that this error occurs:
gethostbyname: Unknown host

- even if it is a perfectly valid host.

I've set everything correct in /etc/resolv.conf etc.

Here's the source code:

--
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    char ipAddress[30];
    struct hostent *h;
    FILE *rfile, *wfile;
    char line[50];

    if(argc<2)
    {
       printf("usage: hostip <file containing host-names>\n");
       exit(1);
    }

    if((rfile=fopen(argv[1],"r")) == NULL)
    {
       printf("Couldn't open \"%s\".\n",argv[1]);
       exit(1);
    }

    wfile=fopen("ip_addresses.txt","a");

    for(;;)
    {
       if(!fgets(line, sizeof(line), rfile))
          break;

          if((h=gethostbyname(line)) == NULL)
          {
             herror("gethostbyname");
             exit(1);
          }

       strcpy(ipAddress,inet_ntoa(*((struct in_addr *)h->h_addr)));
       /* ^ this line also makes an error message:
            warning: passing arg 2 of `strcpy' makes pointer from integer
            without a cast
       */
       fputs(ipAddress,wfile);
    }

   fclose(rfile);
   fclose(wfile);
}
--
I appreciate any help..

Torbjørn Kristoffersen <[EMAIL PROTECTED]>

Reply via email to