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 6692773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:22+00:00 2026-05-26T05:54:22+00:00

Have stumbled upon this code to insert the contents of a file into a

  • 0

Have stumbled upon this code to insert the contents of a file into a vector. Seems like a useful thing to learn how to do:

#include <iostream>
#include <fstream>
#include <vector>

int main() {
   typedef std::vector<char> fileContainer;
   std::ifstream testFile("testfile.txt");
   fileContainer container;
   container.assign(
      (std::istreambuf_iterator<char>(testFile)),
      std::istreambuf_iterator<char>());
    return 0;
}

It works but I’d like to ask is this the best way to do such a thing? That is, to take the contents any file type and insert it into an appropriate STL container. Is there a more efficient way of doing this than above? As i understand, it creates a testFile instance of ifstream and fills it with the contents of testfile.txt, then that copy is again copied into the container through assign. Seems like a lot of copying?

As for speed/efficiency, I’m not sure how to estimate the file size and use the reserve function with that, if i use reserve it appears to slow this code down even. At the moment swapping out vector and just using a deque is quite a bit more efficient it seems.

  • 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-26T05:54:22+00:00Added an answer on May 26, 2026 at 5:54 am

    I’m not sure that there’s a best way, but using the two iterator
    constructor would be more idiomatic:

    FileContainer container( (std::istreambuf_iterator<char>( testFile )),
                             (std::istreambuf_iterator<char>()) );
    

    (I notice that you have the extra parentheses in your assign. They
    aren’t necessary there, but they are when you use the constructor.)

    With regards to performance, it would be more efficient to pre-allocate
    the data, something like:

    FileContainer container( actualSizeOfFile );
    std::copy( std::istreambuf_iterator<char>( testFile ),
               std::istreambuf_iterator<char>(),
               container.begin() );
    

    This is slightly dangerous; if your estimation is too small, you’ll
    encounter undefined behavior. To avoid this, you could also do:

    FileContainer container;
    container.reserve( estimatedSizeOfFile );
    container.insert( container.begin(),
                      std::istreambuf_iterator<char>( testFile ),
                      std::istreambuf_iterator<char>() );
    

    Which of these two is faster will depend on the implementation; the last
    time I measured (with g++), the first was slightly faster, but if you’re
    actually reading from file, the difference probably isn’t measurable.

    The problem with these two methods is that, despite other answers, there
    is no portable way of finding the file size other than by actually
    reading the file. Non-portable methods exist for some systems (fstat
    under Unix), but on other systems, like Windows, there is no means
    of finding the exact number of char you can read from a text file.
    And of course, there’s no guarantee that the results of tellg() will
    even convert to an integral type, and that if it does, that they won’t
    be a magic cookie, with no numerical signification.

    Having said that, in practice, the use of tellg() suggested by other
    posters will often be “portable enough” (Windows and most Unix, at
    least), and the results will often be “close enough”; they’ll usually be
    a little too high under Windows (since the results will count the
    carriage return characters which won’t be read), but in a lot of cases,
    that’s not a big problem. In the end, it’s up to you to decide what
    your requirements are with regards to portability and precision of the
    size.

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

Sidebar

Related Questions

I stumbled upon this piece of code which seems totaly broken to me, but
I'm revisiting som old code of mine and have stumbled upon a method for
I have stumbled into several methods of looping in JavaScript, what I like the
In the Getting things gnome code base I stumbled upon this import statement from
I have been going over rails scheduling tasks options and stumbled upon this piece
I stumbled upon this piece of code today: CGRect rect = {{0,0},{w,h}}; Here, I
I just stumbled upon a change that seems to have counterintuitive performance ramifications. Can
As I'm (finally) learning to code Windows apps in C++ I stumbled upon this:
I stumbled upon this piece of code while reading about DES encryption. I wonder
I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup . How can I

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.