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

  • SEARCH
  • Home
  • 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 6909425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:41:15+00:00 2026-05-27T08:41:15+00:00

How would I do this using STL algorithms? std::ifstream file(filename); std::vector<unsigned char> buf; for(auto

  • 0

How would I do this using STL algorithms?

std::ifstream file(filename);

std::vector<unsigned char> buf;
for(auto file_it = std::istreambuf_iterator<char>(file); file_it != std::istreambuf_iterator<char>() && buf.size() < 2048; ++file_it)
    buf.push_back(*file_it);

Note buf.size() < 2048.

e.g. what will happen if I do the following, and the file is smaller than 2048 bytes?

std::copy_n(std::istreambuf_iterator<char>(file), 2048, std::back_inserter(buf));
  • 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-27T08:41:16+00:00Added an answer on May 27, 2026 at 8:41 am

    Like the documentation says, std::copy_n() will copy exactly n items. It will keep reading past the end of the sequence the iterator refers to. I’m not sure what the standard says about istreambuf_iterator<>, though. It’s probably undefined behavior, but streams are likely to produce lots of copies of eof() past the end. This might lead to lots of garbage when there are less than 2048 bytes available.

    In any case, if you want to reliably copy up to n items, you’ll need to write your own function:

    template<typename I1, typename I2, typename size_type>
    I copy_upto_n ( I1 begin, I1 end, size_type n, I2 out )
    {
        for (size_type i=0; (i < n) && (begin != end); ++i)
        {
            *out++ = *begin++;
        }
        return out;
    }
    

    Some people might use std::iterator_traits<> instead of an extra template parameter to force the same distance type as the iterator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I load an external JavaScript file using a bookmarklet? This would overcome
I enjoy developing algorithms using the STL, however, I have this recurring problem where
I know how I would achieve this using PHP, but with jQuery / JavaScript
I'm not sure how simple this would be, but I'm using a script which
I'm new to jQuery. This would be no problem for me using XPath expressions,
I have a pure C++ application developed using VC 6.0. I would like this
I'm using SQL Server 2005. The query would look like this Select col1, col2,
I would select multiple DOM elements using a typical code such as this: $('#ele1,
I would like to turn this query to regular inline sql without using stored
I have this table structure and would like to map it using Fluent Hibernate

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.