We have a file that has a 64 bit integer as a string in it. How do we scanf() or otherwise parse this numeric string into an unsigned 64 bit integer type in C++ ?
We are aware of things like %lld etc., but a lot of ways to do this parse seem to break compiles under different compilers and stdlibs. The code should compile under gcc and the Microsoft C++ compiler (of course full compliance with standards would be a plus)
Don't use
scanf()
, tokenize your input separately and then usestrtoull()
or similar.GCC has long long, as will compilers for C++0x. MSVC++ doesn't (yet), but does have its __int64 you can use.
Alnitak recommends
strtoull()
, but it seems it's not available in Win32 environments. The linked-to forum thread recommends either of_strtoui64()
,_wcstoui64()
and_tcstoui64()
as replacements. Perhaps this is "on the edge" of stuff that can't really be done with a single portable function call, and you might need to implement different code paths for different platforms. Or, I guess, write your own ASCII-to-64-bit converter, it's not rocket science.Or use the typesafety of istream...