Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Reading an IP Address from a flat text file

I know this isn't terribly difficult, but I am having trouble implementing this.

What I am trying to do is read an IP address from a file and then extract the 4 IP fields and assign them to their corresponding fields (a0,a1,a2,a3).  The IP address file is a flat text file with a single line of code.  It appears as such:

"192.168.0.0"

So I can read in the IP address using the CFile read function, but I am having trouble in extracting the four fields and assigning them the the respective field.

I am able to extract the fields by casting "buffer" (char buffer[256]) to a CString and using the CString functions, but I am having trouble assigning that CString that I extract to a BYTE.  I tried to use the BYTE class valueOf function to convert from CString to BYTE, but I was unsuccessful.

Here is the code snippet.  Down below where I say
        // EXTRACT IP INTS FROM BUFFER TO
        // BYTES a0,a1,a2,a3 HERE
is what I need help filling in.

Any help is greatly appreciated.



// beginning code snippet

// GET IP ADDRESS
CFile IPFile;

// Open the IP address file for reading.
if (IPFile.Open("IPAddr.txt",
    CFile::modeRead | CFile::shareDenyWrite))
     {
     BYTE buffer[256];
     DWORD dwRead;
         
     // Read in 4096-byte blocks,
     // remember how many bytes were actually read.
     dwRead = analyzerIPFile.Read(buffer, 256);
     analyzerIPFile.Close();
         
     // declare fields for ip address
     BYTE a0, a1, a2, a3;
         
        // EXTRACT IP INTS FROM BUFFER TO
        // BYTES a0,a1,a2,a3 HERE



     // set IP Address data bytes
     m_pMyObject->setIPBytes(a0,a1,a2,a3);
     }
// end code snippet
0
bpwallac
Asked:
bpwallac
1 Solution
 
Roshan DavisCommented:
Try this

/*
     Function name     : CPropPageServer::ExtractIPFromString
     Description          : Function for extracting IP byte values from a string.
     Return type          : bool
     Argument          : char* pszIP
     Argument          : DWORD& rdwIP
*/
bool CPropPageServer::ExtractIPFromString(char* pszIP, DWORD& rdwIP)
{
     char *psz = pszIP;
     int nDotCount = 0;
     bool bCheck = true;

     try
     {
          rdwIP = 0;

          while( psz )
          {
               if ( bCheck )
               {
                    rdwIP += atoi(psz);
                    bCheck = false;

                    if ( 3 == nDotCount )
                         break;
                    else
                         rdwIP = rdwIP << 8;
               }

               if ( '.' == *psz )
               {
                    bCheck = true;
                    nDotCount++;
               }

               psz++;
          }

          if ( 3 != nDotCount )
               return false;
     }
     catch(...)
     {
          return false;
     }

     return true;
}


Good Luck
0
 
daknight2000Commented:
say u have a Byte in a CString object like

CString szByte = "123";

why dont u do as follows...

BYTE bByte = (BYTE)atoi((LPCTSTR)szByte);

????


0
 
PriyeshCommented:
//buffer is the string you read in from file.

int nIPByte[4] ;

char* szIPByte = strtok(buffer, ".") ;

int i = 0 ;
while(szIPByte != NULL)
{
  nIPByte[i] = atoi(szIPByte) ;
  i++ ;
}

a0 = nIPByte[0] ;
a1 = nIPByte[1] ;
a2 = nIPByte[2] ;
a3 = nIPByte[3] ;

m_pMyObject->setIPBytes(a0,a1,a2,a3);

or

m_pMyObject->setIPBytes(nIPByte[0], nIPByte[1], nIPByte[2], nIPByte[3]) ;
0
 
PriyeshCommented:
sorry, missed a line of code in while loop.


//buffer is the string you read in from file.

int nIPByte[4] ;

char* szIPByte = strtok(buffer, ".") ;

int i = 0 ;
while(szIPByte != NULL)
{
 nIPByte[i] = atoi(szIPByte) ;
 szIPByte = strtoke(NULL, ".") ;///
 i++ ;
}

a0 = nIPByte[0] ;
a1 = nIPByte[1] ;
a2 = nIPByte[2] ;
a3 = nIPByte[3] ;

m_pMyObject->setIPBytes(a0,a1,a2,a3);

or

m_pMyObject->setIPBytes(nIPByte[0], nIPByte[1], nIPByte[2], nIPByte[3]) ;
0
 
bpwallacAuthor Commented:
Excellent solution!  I totally forgot about the strtok() function, but it is definitely the most simplistic solution in this case.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now