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

查看:192
本文介绍了从文件中读取的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之间打破编译。在code应编译下gcc和微软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天全站免登陆