?
Solved

Reading an IP Address from a flat text file

Posted on 2003-03-02
5
Medium Priority
?
211 Views
Last Modified: 2013-11-20
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
Comment
Question by:bpwallac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 8055730
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
 

Expert Comment

by:daknight2000
ID: 8058807
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
 
LVL 9

Expert Comment

by:Priyesh
ID: 8062019
//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
 
LVL 9

Accepted Solution

by:
Priyesh earned 200 total points
ID: 8062028
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
 

Author Comment

by:bpwallac
ID: 8066450
Excellent solution!  I totally forgot about the strtok() function, but it is definitely the most simplistic solution in this case.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question