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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:05:56+00:00 2026-06-07T01:05:56+00:00

Saving a vector of strings to disk not working as expected. I see that

  • 0

Saving a vector of strings to disk not working as expected.
I see that that file is created but with 0 bytes and no entries.
Note: I compile C++ code on a MAC OS on Xcode (v4.3.3).

Declaration of the vector and the string:

vector<string>taskVector;
string newTaskEntry;

This is how I add new strings to the array while program is running:

taskVector.push_back(newTaskEntry);

When user exit the program I do this:

        fstream file;
        file.open("data.txt", ios::out | ios::trunc);

        for (int i = 0; i > taskVector.size(); i++) {
            file.write(taskVector[i].c_str(), taskVector[i].length());
            file.close();

Any idea why I don’t see any entries? Second of all, can you give me a hint how to read the data back in when I’m able to save the content to disk?

  • 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-07T01:05:58+00:00Added an answer on June 7, 2026 at 1:05 am

    First (like @Nim mentioned) you did your check wrong:

    for (int i = 0; i > taskVector.size(); i++) {
    

    should by

    for (int i = 0; i < taskVector.size(); i++) {
    

    Second: take the close out of the loop:

    for (int i = 0; i > taskVector.size(); i++) {
        file.write(taskVector[i].c_str(), taskVector[i].length());
    }
    file.close();
    

    And about reading them back in:
    Right now, you save no information, where your strings end, or how long they are. Without that, you’ll have to guess, where the strings and (and how many they are). I would recomend one of the following:

    Either take a termination byte, that indicates, when a string ends in your file. '\0' would be an idea. But you must make sure, it is not present in your strings, or you cannot recrate them correctly. When reading back, split at that char.

    The other way would be to store the length information of the strings in front of the strings. You could store an unsigned int of 4 bytes in front befor storing the string. Then, when reading back, you first read back the 4 byte string, then you read the amount of bytes that number indicates. Problems: you need to decide, how long your strings can be at max (with 4 bytes of length information, that’s about 4.8 * 10^9 bytes, pretty much). And you might check on the byte order of your system (big endian vs little endian), if you plan to use it on different computers.

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

Sidebar

Related Questions

I'm saving a struct into a .dat file. Lets suppose I have to edit
I'm saving data to a table, but I also need a column with a
I am currently working on the saving the information using blackberry persistence store. I
i'm not a c++ coder so mayebe it's easy. i have a vector of
I have a vector which accepts vectors of GlDouble vectors. But when I try
I was advised a while ago that is was common place to use std::vector
I'm trying to implement loading and saving for a game I'm working on. What
So I basically have a vector args with 1 argument per array that I'm
I want to read unsigned bytes from a binary file. So I wrote the
hey, I have this code that should save a java.util.Vector of custom serializable classes:

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.