我已经读过,升压iostreams据称支持64位访问大文件的半便携式方式.他们的常见问题解答提到
64 bit offset functions,但没有关于如何使用它们的例子.有没有人用这个库来处理大文件?打开两个文件,寻找他们的middles,并将一个文件复制到另一个文件的一个简单例子将非常有用.
谢谢.
解决方法
简短的回答
只是包括
#include <boost/iostreams/seek.hpp>
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(); }