Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 2.92 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C  : STL: istream_iterator: reading portions of data from file
  2. 5245
  3. 234224
  4. 6534
  5. 1234
  6.        
  7. int main(int argc, char * argv[]) {
  8.   using namespace std;
  9.  
  10.   // 1. prepare the file stream
  11.   string fileName;
  12.   if (argc > 1)
  13.     fileName = argv[1];
  14.   else {
  15.     cout << "Provide the filename to read from: ";
  16.     cin >> fileName;
  17.   }
  18.   unique_ptr<ifstream, ifstream_deleter<ifstream>> ptrToStream(new ifstream(fileName, ios::out));
  19.   if (!ptrToStream->good()) {
  20.     cerr << "Error opening file " << fileName << endl;
  21.     return -1;
  22.   }
  23.  
  24.   // 2. value by value reading will be too slow on large data so buffer data
  25.   typedef unsigned int values_type;
  26.   const int BUFFER_SIZE(4); // 4 is for testing purposes. 16MB or larger in real life
  27.   vector<values_type> numbersBuffer(BUFFER_SIZE);
  28.   numbersBuffer.insert(numbersBuffer.begin(), istream_iterator<values_type>(*ptrToStream), istream_iterator<values_type>());
  29.   // ...
  30.        
  31. std::vector<T> buffer;
  32. buffer.reserve(size);
  33. std::istreambuf_iterator<T> it(in), end;
  34. for (std::vector<T>::size_type count(0), capacity(size);
  35.      it != end && count != capacity; ++it, ++count) {
  36.     buffer.push_back(*it);
  37. }
  38.        
  39. #include <iostream>
  40. #include <iterator>
  41. #include <vector>
  42. #include <sstream>
  43.  
  44. template <typename T>
  45. class counted_istream_iterator:
  46.     public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t>
  47. {
  48. public:
  49.     explicit counted_istream_iterator(std::istream& in): count_(), it_(in) {}
  50.     explicit counted_istream_iterator(size_t count): count_(count), it_() {}
  51.  
  52.     T const& operator*() { return *this->it_; }
  53.     T const* operator->() { return it_->it_.operator->(); }
  54.     counted_istream_iterator& operator++() {
  55.         ++this->count_; ++this->it_; return *this;
  56.     }
  57.     counted_istream_iterator operator++(int) {
  58.         counted_istream_iterator rc(*this); ++*this; return rc;
  59.     }
  60.  
  61.     bool operator== (counted_istream_iterator const& other) const {
  62.         return this->count_ == other.count_ || this->it_ == other.it_;
  63.     }
  64.     bool operator!= (counted_istream_iterator const& other) const {
  65.         return !(*this == other);
  66.     }
  67. private:
  68.     std::ptrdiff_t           count_;
  69.     std::istream_iterator<T> it_;
  70. };
  71.  
  72. void read(int count)
  73. {
  74.     std::istringstream in("0 1 2 3 4 5 6 7 8 9");
  75.     std::vector<int>   vec;
  76.     vec.insert(vec.end(), counted_istream_iterator<int>(in),
  77.                counted_istream_iterator<int>(count));
  78.     std::cout << "size=" << vec.size() << "n";
  79. }
  80.  
  81. int main()
  82. {
  83.     read(4);
  84.     read(100);
  85. }
  86.        
  87. // 2. value by value reading will be too slow on large data so buffer data
  88. typedef unsigned int values_type;
  89. const int BUFFER_SIZE(4);
  90. vector<values_type> numbersBuffer;
  91. numbersBuffer.reserve(BUFFER_SIZE);
  92. istream_iterator<values_type> begin(*ptrToStream), end;
  93. while (begin != end) {
  94.   copy_n(begin, BUFFER_SIZE, numbersBuffer.begin());
  95.   for_each(numbersBuffer.begin(), numbersBuffer.end(), [](values_type const &val){ std::cout << val << std::endl; });
  96.   ++begin;
  97. }
  98.        
  99. 8785
  100. 245245454545
  101. 7767