Home Dashboard Directory Help
Search

std::fstream use 32-bit int as pos_type even on x64 platform by ZephrLee


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 627639
Opened: 12/3/2010 1:35:10 AM
Access Restriction: Public
2
Workaround(s)
view
2
User(s) can reproduce this bug

Description

std::fstream (same for ifstrea, ofstream) defined in STL in VS2010 uses "int" as the type of "pos_type". As the "int" is only 32bit for both "win32" and "x64" platforms, it will fail if we use functions sucha as "tellg" "tellp" "seekg" "seekp" to handle files more than 2GBytes either on "win32" or "x64" platform. Of course, "filebuf::pubseekoff", "filebuf::pubseekpos" also fail.

I look up the stl implementation to find that:
typedef basic_fstream<char, char_traits<char> > fstream;
template<class _Elem,
    class _Traits>
    class basic_fstream
        : public basic_iostream<_Elem, _Traits>
    {    // input/output stream associated with a C stream
public:
                ...
    typedef typename _Traits::pos_type pos_type;
                ...
                }
template<> struct char_traits<char>
    {    // properties of a string or stream char element
                ...
    typedef streampos pos_type;
                ...
                }
typedef fpos<_Mbstatet> streampos;
typedef int _Mbstatet;

It is difficult for me to understand why streampos is defined as int (streamoff is defined as __int64 instead, which is what I expect). As in VC, "int" is always 32-bit and you should use __int64 when you want use 64-bit no matter on "win32" or "x64".

This problem brings me a lot of troubles. There even are not a walk around in fstream, all I can do is turn to FILE* instead (use _fseek64i and _ftell64i). But I would like to use the interface of fstream, so I hope some guys can fix this problem.
Details
Sign in to post a comment.
Posted by Mike Laughton on 2/16/2012 at 1:56 PM
Thanks for your response, Stephan. If this fix is limited to a macro like you mentioned, is there any chance we can get it in the form of a patch or service pack update to VC10?

My team is experiencing this issue, and after finding this page I downloaded VC11 Developer Preview and confirmed that the problem is fixed. Unfortunately since the official VC11 release date is unknown we are in the uncomfortable position of not having a good path forward.

I'm willing to entertain any kind of workaround (even manual patching), as our existing options are not great.

Thanks!
Mike
Posted by Microsoft on 3/3/2011 at 9:24 PM
Hi,

Thanks for reporting this bug. We've fixed it, and the fix will be available in VC11. streampos contains a 32-bit "conversion state", but also contains two 64-bit positions. The problem was that a 32-bit cast was hiding in a macro. We've fixed the cast, so large file support should work correctly now (regardless of x86/x64 platform).

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by Microsoft on 12/3/2010 at 2:20 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Duke Nukem II on 4/13/2012 at 4:09 PM
How to implement Microsoft's fix yourself:

Assuming VC2010 is installed on your drive C:,
In file c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\iosfwd
Change line #65
From: return ((streamoff)(_Myoff + _FPOSOFF(_Fpos)));
To    : return ((streamoff)(_Myoff + _Fpos));

Enjoy :)
Posted by Mike Laughton on 2/16/2012 at 2:03 PM
I rebuilt our code using Visual Studio 11 Developer Preview and the problem was fixed (no code changes necessary)