C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 有人可以提供使用boost iostreams搜索,读取和写入> 4GB文件的示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经读过,升压iostreams据称支持64位访问大文件的半便携式方式.他们的常见问题解答提到 64 bit offset functions,但没有关于如何使用它们的例子.有没有人用这个库来处理大文件?打开两个文件,寻找他们的middles,并将一个文件复制到另一个文件一个简单例子将非常有用.

谢谢.

解决方法

简短的回答

只是包括

#include <boost/iostreams/seek.hpp>

并使用seek功能

boost::iostreams::seek(device,offset,whencE);

哪里

> device是一个文件,流,streambuf或任何可转换为可搜索的对象;
> offset是类型stream_offset的64位偏移量;
> whence是BOOST_IOS :: beg,BOOST_IOS :: cur或BOOST_IOS :: end.

seek的返回值是std :: streampos类型,可以使用position_to_offset函数将其转换为stream_offset.

这是一个冗长乏味且重复的例子,它展示了如何打开两个文件,寻找超过4GB的余量,以及在它们之间复制数据.

警告:此代码将创建非常大的文件(几GB).在支持稀疏文件的OS /文件系统上尝试此示例. Linux还可以;我没有在其他系统上测试它,例如Windows.

/*
 * WARNING: This creates very large files (several GB)
 * unless your OS/file system supports sparse files.
 */
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/positioning.hpp>
#include <cString>
#include <iostream>

using boost::iostreams::file_sink;
using boost::iostreams::file_source;
using boost::iostreams::position_to_offset;
using boost::iostreams::seek;
using boost::iostreams::stream_offset;

static const stream_offset GB = 1000*1000*1000;

void setup()
{
    file_sink out("file1",BOOST_IOS::binary);
    const char *greeTings[] = {"Hello","Boost","World"};
    for (int i = 0; i < 3; i++) {
        out.write(greeTings[i],5);
        seek(out,7*GB,BOOST_IOS::cur);
    }
}

void copy_file1_to_file2()
{
    file_source in("file1",BOOST_IOS::binary);
    file_sink out("file2",BOOST_IOS::binary);
    stream_offset off;

    off = position_to_offset(seek(in,-5,BOOST_IOS::end));
    std::cout << "in: seek " << off << std::endl;

    for (int i = 0; i < 3; i++) {
        char buf[6];
        std::memset(buf,'\0',sizeof buf);

        std::streamsize nr = in.read(buf,5);
        std::streamsize nw = out.write(buf,5);
        std::cout << "read: \"" << buf << "\"(" << nr << "),"
                  << "written: (" << nw << ")" << std::endl;

        off = position_to_offset(seek(in,-(7*GB + 10),BOOST_IOS::cur));
        std::cout << "in: seek " << off << std::endl;
        off = position_to_offset(seek(out,BOOST_IOS::cur));
        std::cout << "out: seek " << off << std::endl;
    }
}

int main()
{
    setup();
    copy_file1_to_file2();
}

大佬总结

以上是大佬教程为你收集整理的c – 有人可以提供使用boost iostreams搜索,读取和写入> 4GB文件的示例全部内容,希望文章能够帮你解决c – 有人可以提供使用boost iostreams搜索,读取和写入> 4GB文件的示例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。