Risposta breve
Proprio includono
#include <boost/iostreams/seek.hpp>
e utilizzare la funzione seek
come in
boost::iostreams::seek(device, offset, whence);
dove
device
è un file, flusso, streamb uf o qualsiasi oggetto convertibile in seekable
;
offset
è un offset a 64 bit di tipo stream_offset
;
whence
è BOOST_IOS::beg
, BOOST_IOS::cur
o BOOST_IOS::end
.
Il valore restituito seek
è di tipo std::streampos
, e può essere convertito in un stream_offset
utilizzando la funzione position_to_offset
.
Esempio
Ecco un lungo, noioso e ripetitivo esempio, che mostra come aprire due file, cercano di offstets> 4 GB, e la copia di dati tra di loro.
AVVISO: questo codice creerà file molto grandi (diversi GB). Prova questo esempio su un sistema operativo/file system che supporta file sparsi. Linux è ok; Non l'ho testato su altri sistemi, come 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();
}
In Windows XP impostazione della funzione a 32 bit() Impossibile creare il file di grandi dimensioni di 2 GB su partizione NTFS. BOOST versione 1.39, MS VS 2008 Express. – Xeningem