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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:08:59+00:00 2026-06-11T20:08:59+00:00

I really didn’t find a satisfied answer at google and I/O in C++ is

  • 0

I really didn’t find a satisfied answer at google and I/O in C++ is a little bit tricky. I would like to read text file by blocks into a vector if possible. Alas, I couldn’t figure out how. I am not even sure, if my infinite loop will be break in all possibilities, because I/O is tricky. So, the best way I was able to figure out is this:

char buffer[1025]; //let's say read by 1024 char block
buffer[1024] = '\0';
std::fstream fin("index.xml");
if (!fin) {
    std::cerr << "Unable to open file";        
} else {
    while (true) {          
        fin.read(buffer, 1024);
        std::cout << buffer;
        if (fin.eof())
            break;
    }

}

Please, note the second line with ‘\0’. Is it not odd? Can I do something better? Can I read the data into the vector instead of char array? Is it appropriate to read into vector directly?

Thanks for your answers.

PS. Reading by chunks have sense indeed. This code is short but I am storing it in cyclic buffer.

  • 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-06-11T20:09:00+00:00Added an answer on June 11, 2026 at 8:09 pm

    You should be fine doing the following

     vector<char> buffer (1024,0);      // create vector of 1024 chars with value 0   
     fin.read(&buffer[0], buffer.size());
    

    The elements in a vector are guaranteed to be stored contiguously, so this should work – but you should ensure that the vector is never empty. I asked a similar question here recently – check the answers to that for specific details from the standard Can I call functions that take an array/pointer argument using a std::vector instead?

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

Sidebar

Related Questions

I have googled this a little and didn't really find the answer I needed.
I have searched a bit on google, but didn't really found an answer to
Well this is a really weird issue, I really didn't find anything on this
[Sorry for the title, I really didn't find a good one, if anyone has
I really didn't know what title I should choose. Anyway, I have code like
I read some articles but really didn't understand what does select 1 from do?
I got asked a question that I really didn't know how to answer. What's
I have read a few articles about .Net Entity Framework that really didn't make
This is almost a duplicate question, but I really didn't understand the answer given
I'd like to apologize for the poor title - I really didn't know how

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.