Respuesta corta
basta con incluir
#include <boost/iostreams/seek.hpp>
y utilizar la función seek
como en
boost::iostreams::seek(device, offset, whence);
donde
device
es un archivo, corriente, streamb uf o cualquier objeto convertible a seekable
;
offset
es un desplazamiento de 64 bits del tipo stream_offset
;
whence
es BOOST_IOS::beg
, BOOST_IOS::cur
o BOOST_IOS::end
.
El valor de retorno de seek
es de tipo std::streampos
, y se puede convertir en un stream_offset
utilizando la función position_to_offset
.
Ejemplo
Aquí se muestra un ejemplo de largo, tedioso y repetitivo, que muestra cómo abrir dos archivos, buscan offstets> 4 GB, y la copia de datos entre ellos.
ADVERTENCIA: Este código creará archivos muy grandes (varios GB). Pruebe este ejemplo en un sistema de archivos/sistema operativo que admita archivos dispersos. Linux está bien; No lo probé en otros sistemas, como 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();
}
En función de la configuración de Windows XP de 32 bits() no puede crear archivos de gran tamaño de 2 GB en una partición NTFS. BOOST versión 1.39, MS VS 2008 Express. – Xeningem