How do you validate that a string is a valid IPv4 address in C++?

前端 未结 17 2099
悲哀的现实
悲哀的现实 2020-12-04 23:21

I don\'t need to validate that the IP address is reachable or anything like that. I just want to validate that the string is in dotted-quad (xxx.xxx.xxx.xxx) IPv4 format, w

17条回答
  •  时光说笑
    2020-12-04 23:59

    If you wanted to write this yourself rather than use a library then

    atoi() to convert characters to ints will let you test the range of each number, and some strcmp's between the "."'s. You can also do some quick checks, such as the length of the string (should be less than 16 characters (not including null terminator), number of dots. etc.

    But, it's probably MUCH easier to use existing code.

提交回复
热议问题