Prints IP string out to console ok, But wont fwrite() them

This is a discussion on Prints IP string out to console ok, But wont fwrite() them within the C Programming forums, part of the General Programming Boards category; OS = Windows XP, Compiler = Microsoft Visual C++ I want to save out the computers various IPs to a ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Prints IP string out to console ok, But wont fwrite() them

    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.
    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
    It gives the following output to the console which is Good.
    Code:
    Output On console
    Computername : themaster
    IP #1:10.0.0.7
    IP #2:192.168.91.1
    IP #3:192.168.40.1
    This is what is acutally saved to log
    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?

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Try writing to the file with fprintf().
    Last edited by sand_man; 03-29-2006 at 05:52 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Instread of using sizeof use strlen to get the length of the string to output. Sizeof(IP) is like saying sizeof(char *)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Quote Originally Posted by sand_man
    Try writing to the file with fprintf().
    Ahh thanks thats what I was looking for fprintf() does the trick nicely. I looked at it for ages and fprintf() never even entered my mind. lol Thanks very much

    Heres the section that needed to be changed.
    Code:
    ip = inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[nCount]);
    fprintf (file, "\nIP:  %-7.16s",ip);	//%-7.16s = min of 7 characters  maximum  16 characters EX: 1.1.1.1 or 255.255.255.255					
    printf("IP #%d: %s\n", nCount++, ip);

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    fyi:

    you could use fprintf exclusively here instead of fwrite and printf additionally.. -for what you are doing here anyway:
    Code:
    //print to file
    fprintf(file, "\nIP:  %-7.16s",ip);
    
    //print to the default standard output pipe
    fprintf(stdout, "IP #%d: %s\n", nCount++, ip);\
    
    //also stderr is another output available..
    //and stdin for reading

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21