OS = Windows XP, Compiler = Microsoft Visual C++
I want to save out the computers various IPs to a log. Here is my Function that works fine up untill saving the actual IPs to the log.
It gives the following output to the console which is Good.Code:void IPaddress() { FILE *file; char *ip; long datasize; // used writing to log WORD wVersionRequested; WSADATA wsaData; char name[255] ={0}; PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 1, 1 ); if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) if( gethostname ( name, sizeof(name)) == 0) { datasize = sizeof (name); // used to write computer name to file printf("Computername: %s\n", name); file=fopen(InfoFile,"a+"); fwrite(name , 1 , datasize , file); // write computer name to file Works Fine fclose(file); // close, could leave open for following code. if((hostinfo = gethostbyname(name)) != NULL) { int nCount = 0; while(hostinfo->h_addr_list[nCount]) { //next I use inet_ntoa which converts the specified Internet host address to a STRING in the Internet standard dot notation. ip = inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[nCount]); printf("IP #%d: %s\n", ++nCount, ip); // This works fine Prints IPs list to console OK file=fopen(InfoFile,"a+"); // Reopen : InfoFile is Defined in Seperate Header fwrite(ip , 1 , sizeof(ip) , file); // Write IPs to file ?? Why cant I save this IP string? // Is it actually a string? fclose(file); } } } }//end
This is what is acutally saved to logCode:Output On console Computername : themaster IP #1:10.0.0.7 IP #2:192.168.91.1 IP #3:192.168.40.1
themaster
So bacically only the computername is actually writen to log, Why dosnt it write IPs part. I am not fully comprhending something. This is my understnding of it .
inet_ntoa() converts the specified Internet host address to a string pointed to by "ip". Then I just fwrite that string to file?



LinkBack URL
About LinkBacks


