C++ fstream 写2G以上大文件有关问题,请大牛们帮忙看看

C++ fstream 写2G以上大文件问题,请大牛们帮忙看看
问题:用tellp()取得当前文件指针位置,回到前面某个位置更改写过的内容。当文件超过2G以后,tellp()取得的文件位置为负,即取得文件位置失败。具体见代码运行结果中current pos的值。顺便讨论下大家在读写大文件的时候一般都用什么策略比较快!!

C/C++ code

int _tmain(int argc, _TCHAR* argv[])
{
    std::fstream m_file;

    m_file.open("test_large_file", std::ios_base::in | std::ios_base::out
        | std::ios_base::trunc | std::ios_base::binary);

    const unsigned int size = 1024*1024*2;
    
    char *data = new char[size];
    for (unsigned int i=0;i<size;++i)
    {
        data[i] = 'a';
    }
    data[size] = 0;
    for (int i=0; i<1030; ++i){
        m_file.write(data,size);
    }
    std::cout<<"size of pos_type:"<<sizeof(std::ios::pos_type)<<std::endl;
    std::cout<<"current pos:"<<m_file.tellp()<<std::endl;
    m_file.flush();
    m_file.close();
    return 0;
}



------解决方案--------------------
fstream能不用就不用,速度算是比较慢的一种文件操作,
要速度快点用fread系列或CreateFile系列函数,
要更快的可以用共享内存文件映射

------解决方案--------------------
STL的fstream支持不了过2G的文件,要操作这么大的文件使用C库函数或者直接使用API
------解决方案--------------------
windows核心编程提供了内存映射文件技术
(Maping File)
http://www.2cto.com/kf/201108/100749.html
------解决方案--------------------
探讨

windows核心编程提供了内存映射文件技术
(Maping File)
http://www.2cto.com/kf/201108/100749.html

------解决方案--------------------
其实Windows下正解是用
_lseeki64
_telli64
------解决方案--------------------
CreateFile吧,最可靠

大文件顺序读写其实不适合mapping,分块映射反而麻烦

只有你觉得用数组寻址和memcpy系列函数明显简化逻辑时才应该用mapping
------解决方案--------------------
_lseeki64
_telli64
32位系统下整数的最大值就是2G,超过2G就变负数了。
------解决方案--------------------
以前处理过8G的数据,也采用过内存映射的方法,但是感觉效果不好。。简直楼主将数据进行压缩,整体载入内存进行操作,或者先将输入进行分割,分块处理。