bpwallac
asked on
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
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
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
}
// end code snippet
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 );
????
CString szByte = "123";
why dont u do as follows...
BYTE bByte = (BYTE)atoi((LPCTSTR)szByte
????
//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(nI PByte[0], nIPByte[1], nIPByte[2], nIPByte[3]) ;
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
or
m_pMyObject->setIPBytes(nI
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent solution! I totally forgot about the strtok() function, but it is definitely the most simplistic solution in this case.
/*
Function name : CPropPageServer::ExtractIP
Description : Function for extracting IP byte values from a string.
Return type : bool
Argument : char* pszIP
Argument : DWORD& rdwIP
*/
bool CPropPageServer::ExtractIP
{
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