Jump to content
  • Advertisement
Sign in to follow this  
Diogo Abreu

How to write a undertemined size array to a txt file?

This topic is 2096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm doing a Text-based RPG game and it will include some checkpoints, on those checkpoints the game will save the progress, so I need to write all my variables to a text file and then read them all when the player loads the game.

Some of my variables, like enemyNames, dont have any determined size, they're like this: "int enemyNames[ ];"

So how do I write an array to a text file without determining a size?
And how do I read them when the player loads the game?

Thank you so much for your help.

Share this post


Link to post
Share on other sites
Advertisement
If you aren't storing the size of the array, you won't be able to use it for anything. You probably do have the number of effective entries, and/or the total number of entries, somewhere. But consider using std::vector<T> rather than raw arrays.

Once you know the size, you can proceed to write the data. There are many ways to do this, it really depends on what constraints there might be, if any, on the data. A simple approach might be to write the number of entries in the list on the current line, and then write each entry on a newline. This will work fine unless you wish to support newlines in the data you are saving.

You should consider using an existing format such as JSON, rather than hand-rolling your own parser and writer.

Share this post


Link to post
Share on other sites
it's just that, not defining the size spares me from the work of changing its size everytime I add a new enemy name for example but if i don't have any option i guess i'll change it..

Share this post


Link to post
Share on other sites
He's saying to use std::vector<T> instead of C arrays. Here, check this out:
#include <iostream>
#include <string>
#include <vector>

int main()
{

    // this is a dynamic array
    std::vector<std::string> enemyNames;
    
    // add as many names to the array as needed
    enemyNames.push_back( "Cubert Cumberdale" );
    enemyNames.push_back( "The Dark Lord" );
    enemyNames.push_back( "ponies" );

    // this will loop through all elements in the array
    for( std::size_t i = 0; i != enemyNames.size(); ++i )
    {

        // you could change this to write to a file
        std::cout << enemyNames[i] << std::endl;
    }

    return 0;
}

Edited by rip-off

Share this post


Link to post
Share on other sites

He's saying to use std::vector<T> instead of C arrays. Here, check this out:

#include <iostream>
#include <string>
#include <vector>

int main()
{

    // this is a dynamic array
    std::vector<std::string> enemyNames;
    
    // add as many names to the array as needed
    enemyNames.push_back( "Cubert Cumberdale" );
    enemyNames.push_back( "The Dark Lord" );
    enemyNames.push_back( "ponies" );

    // this will loop through all elements in the array
    for( std::size_t i = 0; i != enemyNames.size(); ++i )
    {

        // you could change this to write to a file
        std::cout << enemyNames[i] << std::endl;
    }

    return 0;
}

thank you very much, this helps a lot, and when i'm using it in the game itself, i just declare them as i would do with a regular array right? Edited by rip-off

Share this post


Link to post
Share on other sites

This is just bad, bad code design. You should seriously consider a rework of your game structure.

It's pretty pointless to store anything in a "i dont know exactly how many items i need to store" matter.

 

Sure, the example code above will work, even if you'd need a variable which has the number of elements to read, also stored in your file.

If you really want to continue with your approach, check out the previously mentioned JSON format, even XML will do. 

 

If you continue in that hackish way, you'll be very frustraed at some point and you'll end up throwing your project away.

Share this post


Link to post
Share on other sites

... not defining the size spares me from the work of changing its size everytime I add a new enemy name for example...

C++ arrays must have their dimensions set at compile time. You are confused if you believe you can do otherwise. For a fixed array with a dynamic number of active entries, you would need to maintain three variables: the array itself, the total number of elements, and the number of active elements.

... when i'm using it in the game itself, i just declare them as i would do with a regular array right?

More or less. Instead of writing T myContainer[N], you would use std::vector<T> myContainer.

When passing to functions, you may want to use (const) reference to avoid unnecessary copies, e.g:
// When this function is called, a vector is temporarily created, and the data is copied from the source
// When the function returns, the temporary data is destroyed
void frobnicateCopy(std::vector<Example> data) {
    // frobnication...
}

// No copies or significant work is done when this function is called or returns
// The data is fetched (indirectly) from the source vector
void frobnicateReference(const std::vector<Example> &data) {
    // frobnication...
}
Note that raw C++ array parameters in function signatures are actually pointers in disguise, so any functions you were writing with your raw arrays were effectively passed by reference like the latter style above.
// The following declarations are actually identical

void frobnicate(Example data[], int totalElements) {
    // frobnication...
}

void frobnicate(Example *data, int totalElements) {
    // frobnication...
}

Share this post


Link to post
Share on other sites

This is just bad, bad code design. You should seriously consider a rework of your game structure.

It's pretty pointless to store anything in a "i dont know exactly how many items i need to store" matter.

 

Sure, the example code above will work, even if you'd need a variable which has the number of elements to read, also stored in your file.

If you really want to continue with your approach, check out the previously mentioned JSON format, even XML will do. 

 

If you continue in that hackish way, you'll be very frustraed at some point and you'll end up throwing your project away.

i'll just change it then, it's not too late...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!