Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:45:31+00:00 2026-05-30T04:45:31+00:00

Target: There is text file (on HDD) containing integers divided with some kind of

  • 0

Target: There is text file (on HDD) containing integers divided with some kind of delimiter.

Example:

5245
234224
6534
1234

I need to read them into STL container.

int main(int argc, char * argv[]) {
  using namespace std;

  // 1. prepare the file stream
  string fileName;
  if (argc > 1)
    fileName = argv[1];
  else {
    cout << "Provide the filename to read from: ";
    cin >> fileName;
  }
  unique_ptr<ifstream, ifstream_deleter<ifstream>> ptrToStream(new ifstream(fileName, ios::out));
  if (!ptrToStream->good()) {
    cerr << "Error opening file " << fileName << endl;
    return -1;
  }

  // 2. value by value reading will be too slow on large data so buffer data
  typedef unsigned int values_type;
  const int BUFFER_SIZE(4); // 4 is for testing purposes. 16MB or larger in real life
  vector<values_type> numbersBuffer(BUFFER_SIZE);
  numbersBuffer.insert(numbersBuffer.begin(), istream_iterator<values_type>(*ptrToStream), istream_iterator<values_type>());
  // ...

The main drawback of this code is how can I handle the issue when file size is extremely large, so I cannot store all of it’s contents in memory ?
I also do not want to use push_back as it is non efficient in comparison to interval insert.


So, the question is: how can I read not more than BUFFER_SIZE elements from the file effectively using STL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:45:33+00:00Added an answer on May 30, 2026 at 4:45 am

    The approach to limit reading from input iterators is to create a wrapper which counts the number of elements processed so far and whose end iterator compares to this number. Doing this generically isn’t quite trivial, doing it specifically for std::istream_iterator<T> shouldn’t be too hard. That said, I think the easiest way to do it is this:

    std::vector<T> buffer;
    buffer.reserve(size);
    std::istreambuf_iterator<T> it(in), end;
    for (std::vector<T>::size_type count(0), capacity(size);
         it != end && count != capacity; ++it, ++count) {
        buffer.push_back(*it);
    }
    

    I realize that you don’t want to push_back() because it is allegedly slow. However, compared to the I/O operation I doubt that you’ll be able to measure the small overhead, especially with typical implementation of the I/O library.

    Just to round things off with an example of a wrapped iterator: below is an example how a counting wrapper for std::istream_iterator<T> could look like. There are many different ways this could be done, this is just one of them.

    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <sstream>
    
    template <typename T>
    class counted_istream_iterator:
        public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t>
    {
    public:
        explicit counted_istream_iterator(std::istream& in): count_(), it_(in) {}
        explicit counted_istream_iterator(size_t count): count_(count), it_() {}
    
        T const& operator*() { return *this->it_; }
        T const* operator->() { return it_->it_.operator->(); }
        counted_istream_iterator& operator++() {
            ++this->count_; ++this->it_; return *this;
        }
        counted_istream_iterator operator++(int) {
            counted_istream_iterator rc(*this); ++*this; return rc;
        }
    
        bool operator== (counted_istream_iterator const& other) const {
            return this->count_ == other.count_ || this->it_ == other.it_;
        }
        bool operator!= (counted_istream_iterator const& other) const {
            return !(*this == other);
        }
    private:
        std::ptrdiff_t           count_;
        std::istream_iterator<T> it_;
    };
    
    void read(int count)
    {
        std::istringstream in("0 1 2 3 4 5 6 7 8 9");
        std::vector<int>   vec;
        vec.insert(vec.end(), counted_istream_iterator<int>(in),
                   counted_istream_iterator<int>(count));
        std::cout << "size=" << vec.size() << "\n";
    }
    
    int main()
    {
        read(4);
        read(100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the below example. There, I have: target MAIN calls target t and
I'm trying to parse some data in a fixed format text file where each
Is there a way to write python data structs to a file as text.
I have made an actionscript that loads an external text file, and scrolls its
I have a text file located on the server that contains a list of
I'm wondering if there is a way to write MIPS assembly language file (just
So there's an XSD schema that validates a data file. It declares root element
In a Flash game I am developing, there are some settings that are set
I'm trying to read the contents of a text file into the attributes of
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.