Is std::streampos guaranteed to be unsigned long long?

自作多情 提交于 2019-11-30 07:31:51

问题


Is std::streampos guaranteed to be unsigned long long?

If not so, how does std::istream::seekg work correctly on files larger than 4G?


回答1:


From http://en.cppreference.com/w/cpp/io/fpos:

std::streampos is a specialization of the class template

template< class State > class fpos;

std::streampos is typedef'ed to be std::fpos<std::char_traits<char>::state_type>

Each object of type fpos holds the byte position in the stream (typically as a private member of type std::streamoff).

From http://en.cppreference.com/w/cpp/io/streamoff:

The type std::streamoff is a signed integral type of sufficient size to represent the maximum possible file size supported by the operating system. Typically, this is a typedef to long long.

To answer your questions...

Question Is std::streampos guaranteed to be unsigned long long?

Answer I am sure you meant to find out whether the underlying integral type that holds the position is guaranteed to be unsigned long long. In that sense, the real question is whether std::streamoff is gueranteed to be unsigned long long. The answer to that question is "No", as you can infer from the descriptions above.

Question If not so, how does std::istream::seekg work correctly on files larger than 4G?

Answer If an operating system supports working with files larger than 4G, it's std::streamoff is typdef'ed accordingly. Even then, it is most likely going to be a signed integral type. Here's another quote from http://en.cppreference.com/w/cpp/io/streamoff.

A std::streamoff value of -1 is also used to represent error conditions by some of the I/O library functions.




回答2:


No, it's not guaranteed to be unsigned long long.

Especially with older compilers, it may not work with files larger than 4 GB (and in a few cases it was signed, so it only worked with files up to 2 GB--but when typical hard drives were 20 to 40 megabytes, that probably didn't seem like a major consideration).



来源:https://stackoverflow.com/questions/24437016/is-stdstreampos-guaranteed-to-be-unsigned-long-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!