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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:43:03+00:00 2026-06-01T13:43:03+00:00

I am working on a problem, and I am not sure if I’m doing

  • 0

I am working on a problem, and I am not sure if I’m doing this properly, or if I even have the right approach at all. My code, so far, just gives me segfaults.

I have a simple little receipt writing program to practice some C++ concepts, one of them being file i/o in binary. I have a couple arrays holding data (one holding strings of a service type, the other holding corresponding double costs). I have a little menu, and the writing in binary part goes fine. However, I am not sure how to read these back from the file.

I am currently using a structure to hold various data members (customer’s name, total charges, etc.), and I want to write all of the services they chose, along with their corresponding prices, to a file, and then be able to read them back. These are currently stored in vectors in the structure.

I think I understand how to read/write strings to/from a binary file (write the length of the string, then the data, then read the length back, then read that many bytes into a string). But how do I do this with a vector? I understand how it can be done with, say, a vector of fixed-size members (i.e. ints), but what about a vector of strings? In this case, the members do not have a set size, so I am not sure how to read them back.

Is this even possible? If anyone could point me in the right direction, that would be extremely helpful. There was a similar forum posting a year or so ago, but it didn’t help me much.

tl;dr — How do I read a variable-size vector of variable-length strings from a binary file?

  • 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-01T13:43:04+00:00Added an answer on June 1, 2026 at 1:43 pm

    To paraphrase:

    > How do I read the length back, then read that many bytes into a string with a vector?

    One string at a time:

    // Untested
    std::vector<std::string> ReadSomeStrings(std::istream& inFile, size_t howMany) {
      std::vector<std::string> result;
      for(int i = 0; i < howMany; ++i) {
        std::size_t len;
        is.read(&len, sizeof len);
        std::string s(len, '\0');
        is.read(&s[0], s.size());
        result.push_back(s);
      }
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

not sure why this isn't working-- i have 3 divs where the two on
Edit I got this working, I'm not sure if this is the right way
I got this problem with AVAudioRecorder not working for me, at least from what
The code below is working, but with a problem I do not understand. When
jquery-loadmask not working properly in IE8 I got a same problem. It was issue
I have working registration script the only problem is that i do not know
Does anyone see a problem with this, its not working saying bad file descriptor
This was working just fine, not sure what broke it, but now I'm getting
I am not sure why my child selector isn't working properly. In my example,
First time using wordpress query so not sure how to handle this problem. 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.