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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:47:39+00:00 2026-05-10T17:47:39+00:00

In order to improve performance reading from a file, I’m trying to read the

  • 0

In order to improve performance reading from a file, I’m trying to read the entire content of a big (several MB) file into memory and then use a istringstream to access the information.

My question is, which is the best way to read this information and ‘import it’ into the string stream? A problem with this approach (see bellow) is that when creating the string stream the buffers gets copied, and memory usage doubles.

#include <fstream> #include <sstream>  using namespace std;  int main() {   ifstream is;   is.open (sFilename.c_str(), ios::binary );    // get length of file:   is.seekg (0, std::ios::end);   long length = is.tellg();   is.seekg (0, std::ios::beg);    // allocate memory:   char *buffer = new char [length];    // read data as a block:   is.read (buffer,length);    // create string stream of memory contents   // NOTE: this ends up copying the buffer!!!   istringstream iss( string( buffer ) );    // delete temporary buffer   delete [] buffer;    // close filestream   is.close();    /* ==================================    * Use iss to access data    */  } 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T17:47:40+00:00Added an answer on May 10, 2026 at 5:47 pm

    std::ifstream has a method rdbuf(), that returns a pointer to a filebuf. You can then ‘push’ this filebuf into your stringstream:

    #include <fstream> #include <sstream>  int main() {     std::ifstream file( 'myFile' );      if ( file )     {         std::stringstream buffer;          buffer << file.rdbuf();          file.close();          // operations on the buffer...     } } 

    EDIT: As Martin York remarks in the comments, this might not be the fastest solution since the stringstream‘s operator<< will read the filebuf character by character. You might want to check his answer, where he uses the ifstream‘s read method as you used to do, and then set the stringstream buffer to point to the previously allocated memory.

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

Sidebar

Ask A Question

Stats

  • Questions 277k
  • Answers 277k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Aha! Figured it out. I needed to write a TypeConverter… May 13, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer To add some explanation to the code posted by ChaosPandion,… May 13, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer How do you send the response back? If you return… May 13, 2026 at 3:00 pm

Related Questions

In order to improve performance reading from a file, I'm trying to read the
I know that Java has beautiful inbuilt support for the HashMaps or HashTables. Does
I was wondering if it is possible to tweak the way garbage collector works
My application has to deal with large amounts of data, usual select size is
The way i currently populate business objects is using something similar to the snippet

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.