有人可以提供使用boost iostreams搜索,读取和写入> 4GB文件的示例

Que*_*ess 7 c++ large-files boost-iostreams

我已经读过,升压iostreams据称支持64位访问大文件的半便携式方式.他们的常见问题解答提到64位偏移函数,但没有关于如何使用它们的例子.有没有人用这个库来处理大文件?打开两个文件,寻找中间文件,将一个文件复制到另一个文件的简单示例将非常有用.

谢谢.

Dan*_*nga 7

简短的回答

只是包括

#include <boost/iostreams/seek.hpp>
Run Code Online (Sandbox Code Playgroud)

并使用该seek功能

boost::iostreams::seek(device, offset, whence);
Run Code Online (Sandbox Code Playgroud)

哪里

  • device是一个文件,流,streambuf或任何可转换为的对象seekable;
  • offset是64位的类型偏移stream_offset;
  • whenceBOOST_IOS::beg,BOOST_IOS::curBOOST_IOS::end.

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

这是一个冗长乏味且重复的例子,它展示了如何打开两个文件,寻找超过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, 7*GB, BOOST_IOS::cur));
        std::cout << "out: seek " << off << std::endl;
    }
}

int main()
{
    setup();
    copy_file1_to_file2();
}
Run Code Online (Sandbox Code Playgroud)