Ho letto che gli iostream di boost supportano apparentemente l’accesso a 64 bit a file semi-portatili di grandi dimensioni. Le loro FAQ menzionano le funzioni di offset a 64 bit , ma non ci sono esempi su come usarle. Qualcuno ha usato questa libreria per gestire file di grandi dimensioni? Un semplice esempio di apertura di due file, cercando i loro media, e copiarli uno con l’altro sarebbe molto utile.
Grazie.
Basta includere
#include
e utilizzare la funzione di seek
come in
boost::iostreams::seek(device, offset, whence);
dove
device
è un file, stream, streambuf o qualsiasi object convertibile a seekable
; offset
è un offset
a 64 bit di tipo stream_offset
; whence
è BOOST_IOS::beg
, BOOST_IOS::cur
o BOOST_IOS::end
. Il valore di ritorno di seek
è di tipo std::streampos
, e può essere convertito in stream_offset
usando la funzione position_to_offset
.
Ecco un esempio lungo, noioso e ripetitivo, che mostra come aprire due file, cercare di cancellare> 4 GB e copiare i dati tra loro.
ATTENZIONE: 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 #include #include #include 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(); }