Read 64bit whole line from file

We have a file with a 64 bit integer as a string. How do we scan () or otherwise parse this numeric string into an unsigned 64-bit integer type in C ++?

We know about things like% lld, etc., but many ways to do this syntax seem to break compilations under different compilers and stdlib. The code should compile under gcc and the Microsoft C ++ compiler (of course, full compliance with the standards would be a plus)

+3
source share
5 answers

GCC has been around for a long time, like compilers for C ++ 0x. MSVC ++ does not yet exist, but has its own __int64, which you can use.

#if (__cplusplus > 199711L) || defined(__GNUG__)
    typedef unsigned long long uint_64_t;
#elif defined(_MSC_VER) || defined(__BORLANDC__) 
    typedef unsigned __int64 uint_64_t;
#else
#error "Please define uint_64_t"
#endif

uint_64_t foo;

std::fstream fstm( "file.txt" );
fstm >> foo;
+5
source

Alnitak strtoull(), , , Win32. _strtoui64(), _wcstoui64() _tcstoui64() . , " " , , . , , ASCII-to-64-bit, .

+4

istream...

  using namespace std;

  // construct a number -- generate test data
  long long llOut = 0x1000000000000000;
  stringstream sout;
  // write the number
  sout << llOut;
  string snumber = sout.str();
  // construct an istream containing a number
  stringstream sin( snumber );

  // read the number -- the crucial bit
  long long llIn(0);
  sin >> llIn;
+3
std::fstream fstm( "file.txt" );
__int64 foo;
fstm >> foo;
+2

scanf(), , strtoull() .

+1

Source: https://habr.com/ru/post/1698956/


All Articles