Matching strings in C ++ (hostname and port)

I want to highlight const char * hostName as "hostName: port" in const char * hostNameFinal and number port.

I have the following code:

const char* hostName = "localhost:643246";

long int port;
char hostNameChar[256];
sscanf(hostName, "%s:%d", hostNameChar, &port);

Output hostNameChar: localhost: 643246 Port output is crazy but not 643246

Sometimes the port value is too large, what data type should I use? How can I match the host name correctly to get two variables with the necessary information?

+3
source share
6 answers

Since you have C ++ in the title of the question, I would suggest manually deleting char arrays and using std :: string.

#include <string>
#include <sstream>

std::string hostName = "localhost:643246";

size_t colonPos = hostName.find(':');

if(colonPos != std::string::npos)
{
     std::string hostPart = hostName.substr(0,colonPos);
     std::string portPart = hostName.substr(colonPos+1);

     std::stringstream parser(portPart);

     int port = 0;
     if( parser >> port )
     {
          // hostname in hostPart, port in port
          // could check port >= 0 and port < 65536 here
     }
     else
     {
         // port not convertible to an integer
     }
}
else
{
    // Missing port?
}
+4
source

sscanf %s ; , . , : : hostName strchr sscanf (, , - atoi), . unsigned int long ; a int - , 16- ( , , , ).

(643246 , - 16 , 0 65535.)

EDITED , , :

const char * colon = strchr(hostName, ':');
memcpy(hostNameChar, hostName, colon-hostName);
hostNameChar[colon-hostName];
port = atoi(colon+1);

EDITED , atoi . , (1) strchr, , , (2) , , 256 ( hostNameChar - ), (3) strtol, atoi, (4) strtol, , , (5) kinda-return-value strtol, , . .

+1

:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const char* hostName = "localhost:643246";

    long int port;
    char hostNameChar[256];
    if (sscanf(hostName, "%[^:]:%d", hostNameChar, &port) != 2)
    {
        // It did not work.
        // scanf() returns the number of matched tokens.
        fprintf(stdout, "Fail\n");
        exit(1);
    }
    fprintf(stdout, "Host(%s) Port(%d)\n", hostNameChar, port);
}

, % s . . % [<BLA> ] , <BLA> . ^. , <BLA> .

+1

You can use UrlGetPart http://msdn.microsoft.com/en-us/library/bb773781(v=VS.85).aspx

If the buffer is too small, E_POINTER is returned, and the value pointed to by pcchOut will be set to the minimum number of characters that the buffer must contain, including the terminating NULL character.

0
source

Another solution in C ++ (awful compared to C alone :)

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
  const char* hostName = "localhost:643246";
  long int port;
  char hostNameChar[256];

  istringstream iss(hostName);
  string hostNameString;
  getline(iss, hostNameString, ':');
  strcpy(hostNameChar, hostNameString.c_str());
  iss >> port;
  cout << hostNameChar << "-" << port << endl;
}
0
source

Try the following:

sscanf(hostName, "%s:%ld", hostNameChar, &port);

ld = long signed int

However, I think the port number cannot be> 65536

-1
source

All Articles