[mfc] ip address control
-
hello, i want to set the value of the ip address control from a string, say ip[100] which is read from a text file. how this can be done ? the text file contains ip like --- 127.0.0.1 or 111.111.11.1
ULONG ulP = inet_addr(szIPAddress );
-
ULONG ulP = inet_addr(szIPAddress );
i dont get, please explain. i am using this code but it makes the mfc application crash.
len = strlen(fff); if(strlen(fff) > 0 && strcmp(fff, "\\n") != 0) { while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m1 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m2 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m3 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(len > i) { c\[j\] = fff\[i\]; j++; i++; } m4 = atoi(c); j = 0; i++; ZeroMemory(c, 3); ipAdd.SetAddress(m1, m2, m3, m4); }
-
hello, i want to set the value of the ip address control from a string, say ip[100] which is read from a text file. how this can be done ? the text file contains ip like --- 127.0.0.1 or 111.111.11.1
I assume you are talking about
CIPAddressCtrl
. The methodCIPAddressCtrl::SetAddress
take 4 BYTE parameters (see : doc on MSDN[^]) So, you will need to extract each "part" from the string and convert each substring to a BYTE. (manually or int_addr[^])This signature was proudly tested on animals.
-
i dont get, please explain. i am using this code but it makes the mfc application crash.
len = strlen(fff); if(strlen(fff) > 0 && strcmp(fff, "\\n") != 0) { while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m1 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m2 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(fff\[i\] != '.') { c\[j\] = fff\[i\]; j++; i++; } m3 = atoi(c); j = 0; i++; ZeroMemory(c, 3); while(len > i) { c\[j\] = fff\[i\]; j++; i++; } m4 = atoi(c); j = 0; i++; ZeroMemory(c, 3); ipAdd.SetAddress(m1, m2, m3, m4); }
You may be over complicating the conversion of the 4 number parts of the IP address. Try this instead:
int m1,m2,m3,m4; sscanf(fff,"%d.%d.%d.%d",&m1,&m2,&m3,&m4); ipAdd.SetAddress(m1,m2,m3,m4);
Of course, you'll need to validate the IP address is in the proper format.
-
You may be over complicating the conversion of the 4 number parts of the IP address. Try this instead:
int m1,m2,m3,m4; sscanf(fff,"%d.%d.%d.%d",&m1,&m2,&m3,&m4); ipAdd.SetAddress(m1,m2,m3,m4);
Of course, you'll need to validate the IP address is in the proper format.
-
The variable fff must be a string only containing a valid IP address. try this:
LPSTR lpIPAddress = _T("127.0.0.1");
int m1, m2, m3, m4;
sscanf(lpIPAddress,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
ipAdd.SetAddress(m1,m2,m3,m4);//You should now see 127.0.0.1 in the IPAddress control
Make sure you're passing in just the IP address in the sscanf function.
-
The variable fff must be a string only containing a valid IP address. try this:
LPSTR lpIPAddress = _T("127.0.0.1");
int m1, m2, m3, m4;
sscanf(lpIPAddress,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
ipAdd.SetAddress(m1,m2,m3,m4);//You should now see 127.0.0.1 in the IPAddress control
Make sure you're passing in just the IP address in the sscanf function.
-
it is working now. since i am using
fileh.ReadString((LPTSTR)fff , 100);
therefore i have to use the following code for the correct result ---
swscanf((LPCTSTR)fff,_T("%d.%d.%d.%d"),&m1,&m2,&m3,&m4);
thanks for the help.
You're welcome! Glad you got it working. :-D