从文件中读取 64 位整数字符串 [英] Read 64 bit integer string from file

查看:26
本文介绍了从文件中读取 64 位整数字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个文件,其中包含一个 64 位整数作为字符串.我们如何 scanf() 或以其他方式将此数字字符串解析为 C++ 中的无符号 64 位整数类型?

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++ ?

我们知道诸如 %lld 之类的东西,但是很多进行这种解析的方法似乎会破坏在不同编译器和 stdlib 下的编译.代码需要在 gcc 和 Microsoft 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)

推荐答案

GCC 由来已久,C++0x 的编译器也由来已久.MSVC++ 没有(还),但有它的 __int64 你可以使用.

GCC has long long, as will compilers for C++0x. MSVC++ doesn't (yet), but does have its __int64 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;

这篇关于从文件中读取 64 位整数字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆