2009-08-19 10 views
6

का उपयोग करके एक> 4 जीबी फ़ाइल को खोजने, पढ़ने और लिखने का उदाहरण प्रदान कर सकता है, मैंने पढ़ा है कि बूस्ट iostreams बड़ी फ़ाइलों को सेमी-पोर्टेबल तरीके से 64 बिट एक्सेस का समर्थन करता है। उनके एफएक्यू में 64 bit offset functions का उल्लेख है, लेकिन इनका उपयोग करने के तरीके पर कोई उदाहरण नहीं है। क्या किसी ने बड़ी फाइलों को संभालने के लिए इस पुस्तकालय का उपयोग किया है? दो फाइलें खोलने का एक सरल उदाहरण, उनकी मिडल की तलाश करना, और एक दूसरे को कॉपी करना बहुत उपयोगी होगा।क्या कोई बूस्ट iostreams

धन्यवाद।

उत्तर

7

लघु जवाब

बस

#include <boost/iostreams/seek.hpp> 

शामिल हैं और के रूप में में

boost::iostreams::seek(device, offset, whence); 

जहां

  • device एक फ़ाइल, धारा, streamb है seek फ़ंक्शन का उपयोग करें uf या किसी ऑब्जेक्ट को परिवर्तनीय seekable;
  • offsetstream_offset प्रकार का 64-बिट ऑफ़सेट है;
  • whenceBOOST_IOS::beg, BOOST_IOS::cur या BOOST_IOS::end है।

seek की वापसी मान प्रकार std::streampos की है, और यह position_to_offset समारोह का उपयोग कर एक stream_offset में बदला जा सकता।

उदाहरण

यहाँ एक, लंबे समय से थकाऊ और दोहराव उदाहरण है, जो कैसे दो फ़ाइलें खोलने के लिए, उन दोनों के बीच offstets> 4GB, और नकल डेटा की तलाश से पता चलता है।

चेतावनी: यह कोड बहुत बड़ी फाइलें (कई जीबी) बनाएगा। इस उदाहरण को ओएस/फाइल सिस्टम पर आज़माएं जो स्पैस फाइलों का समर्थन करता है। लिनक्स ठीक है; मैंने विंडोज़ जैसे अन्य सिस्टमों पर इसका परीक्षण नहीं किया।

/* 
* 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(); 
} 
+0

विंडोज एक्सपी 32-बिट फ़ंक्शन सेटअप() के तहत NTFS विभाजन पर 2 जीबी से बड़ी फ़ाइल नहीं बना सकता है। बूस्ट संस्करण 1.3 9, एमएस वीएस 2008 एक्सप्रेस। – Xeningem

संबंधित मुद्दे