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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:07:13+00:00 2026-05-25T16:07:13+00:00

I want to read N bytes of data from a file stream and append

  • 0

I want to read N bytes of data from a file stream and append them to a vector. So let’s say we have a

basic_ifstream<uint8_t> myFileStream;

and a

vector<uint8_t> myBuffer;

Currently I’m doing something like this:

myBuffer.reserve(N);
for (int i=0; i<N; ++i)
{
    uint8_t tmpByte;
    myFileStream.read(&tmpByte, 1);
    myBuffer.push_back(tmpByte);
}

but this is extremely slow.

Now I tried to let myFileStream.read copy the data directly into the vector. Since a vector stores its elements in a contiguous storage location, I thought that something like this should be possible:

uint8_t* ptr = &myBuffer.back(); // there is already some elements in the buffer (I know)
ptr++; // first element after existing data
myBuffer.resize(myBuffer.size() + N);
myFileStream.read(ptr, N);

But with this I get a runtime error (corruption of heap). What is wrong with this solution? Or is there a better way to do this anyway?

  • 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-25T16:07:14+00:00Added an answer on May 25, 2026 at 4:07 pm

    Your problem is that resize may need to reallocate the whole vector, and thus invalidate your previous ptr. You need to take the pointer only after resize.

    std::size_t oldSize = myBuffer.size();
    // resize first
    myBuffer.resize(oldSize + N);
    uint8_t* ptr = &myBuffer[oldSize]; // already first element after existing data
    myFileStream.read(ptr, N);
    

    Note that as a bonus this implementation will work even if the original vector is empty (for N != 0, of course).

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

Sidebar

Related Questions

I want to read each line from a text file and store them in
H, How to Read First 512 Bytes of data from a .dat file in
I want to read unsigned bytes from a binary file. So I wrote the
I have a text file. I want read that file. But In that if
I want to read and write from serial using events/interrupts. Currently, I have it
I want to read line n1->n2 from file foo.c into the current buffer. I
I am trying to read in the pixel data from an image file as
i'd like to read binary data from a blob, using the Stream interface around
I'm trying to read data in from a binary file and then store in
I want to read an xml file, apply a transform, then write to another

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.