Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

c++ - Is std::streampos guaranteed to be unsigned long long?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...