I want to parse a string of the form 127.0.0.1 Down56 to ip and port. Do the following
#define DEFAULT_INTERFACE "0.0.0.0" #define DEFAULT_PORT 27000 const char* iface = DEFAULT_INTERFACE; int port = DEFAULT_PORT; size_t colonPos = str.find(":"); port = atoi(str.substr(colonPos + 1, str.size() - colonPos).c_str()); std::cout << "test1: " << argumentValue.substr(0, colonPos).c_str() << std::endl; iface = argumentValue.substr(0, colonPos).c_str(); std::cout << "test2: iface " << iface << " port " << port << std::endl;
The conclusion is the following
test1: 127.0.0.1
test2: iface port 556
Why iface is an empty string? This is for Windows. Under Linux, it works fine, but if you first get iface, then port, then both values are 556
str
, then withargumentValue
, but apparently it’s necessary. but all the extra letters (substr, etc. here to anything). - avp