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
bpwallacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bpwallacAuthor Commented:
Excellent solution!  I totally forgot about the strtok() function, but it is definitely the most simplistic solution in this case.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.