How do I tell if the c function atoi failed or if it was a string of zeros?

后端 未结 6 1477
悲&欢浪女
悲&欢浪女 2020-11-28 09:52

When using the function atoi (or strtol or similar functions for that matter), how can you tell if the integer conversion failed or if the C-string

6条回答
  •  没有蜡笔的小新
    2020-11-28 10:19

    It's been a while since I've done and C/C++, but it would appear to me that the (overly) simple solution would be to check just the string for "0".

    int value = atoi(string_number.c_str());
    
    if ( !value && string_number != "0" ) {
      // error
    } else {
      // great success!
    }
    

提交回复
热议问题