fork(1) download
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <cerrno>
  4. #include <cstring>
  5.  
  6. int main()
  7. {
  8. char str[] = " 2 365 2344jim 1234444444444444444444567 43";
  9.  
  10. for (char * e = str; *e != '\0'; )
  11. {
  12. errno = 0;
  13. char const * s = e;
  14. unsigned long int n = strtoul(s, &e, 0);
  15.  
  16. if (errno) // conversion error (e.g. overflow)
  17. {
  18. std::printf("Error (%s) encountered converting:%.*s.\n",
  19. std::strerror(errno), e - s, s);
  20. continue;
  21. }
  22.  
  23. if (e == s) { ++e; continue; } // skip inconvertible chars
  24.  
  25. s = e;
  26.  
  27. printf("We read: %lu\n", n);
  28. }
  29. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
We read: 2
We read: 365
We read: 2344
Error (Numerical result out of range) encountered converting: 1234444444444444444444567.
We read: 43