Jump to content
  • Advertisement
Sign in to follow this  
darenking

How to pass a pointer to a filestream?

This topic is 4944 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

Hello! I am writing a game, though I didn't put this in the gaming forum as the problem isn't a game specific thing. I have a class that loads data (map data for my game). One method, LoadMap, opens a filestream and loads in the number of layers that make up the map. The method then calls a second method once for each of these layers, to load in the data for that particular layer. The problem of course is, how can the second method, the one that loads the actual map data, access the filestream? Here is my code, a simplified version: void World::LoadMap() { std::ifstream file( "filename.dat", std::ios::binary ); //load amount of layers int amount; file.read( (char*) &amount, sizeof(int) ); //load layer data for all layers LoadMapLayer(0);// will be put into a for loop later, using 'amount' LoadMapLayer(1); LoadMapLayer(2); file.close(); return; } void World::LoadMapLayer(int layer) { // the code in here just loads in the data for the particular map layer // problem is, how can it access the filestream from the previous function? }

Share this post


Link to post
Share on other sites
Advertisement
You could just close it then open it in each LoadLayer. It should be easy to find your place in the file assuming you know the length of a layer. Another way to do it since I see you're using binary is to just dump your whole map struct in and read it back out. As long as there is no pointers in it that is.

On a side note I'm guessing your making a tile based game, is there a reason you want a variable number of layers?

Share this post


Link to post
Share on other sites
pass it a reference to the stream;

void World::LoadMapLayer(std::ifstream &file,int layer)
{
// the code in here just loads in the data for the particular map layer
// problem is, how can it access the filestream from the previous function?
}

LoadMapLayer(file,0);

Share this post


Link to post
Share on other sites

Does my 'member function definition' in the header file look like this:
void LoadMapLayer(std::ifstream &file, int layer);

...assuming I go with _the_phantom_'s suggestion, this:
void World::LoadMapLayer(std::ifstream &file,int layer)
{
}
...and call the function like this:
LoadMapLayer(file,0);

Share this post


Link to post
Share on other sites
Quote:
Original post by joebarnslondon

Does my 'member function definition' in the header file look like this:
void LoadMapLayer(std::ifstream &file, int layer);


Yah, but its called declaration its not a definition [grin].

You could generalize it if you wanted to so you could use other kind of streams


void LoadMapLayer(std::istream& in, int layer);


or go over the top [grin]:


template < typename CharT, typename Traits >
void LoadMapLayer(std::basic_istream<CharT, Traits>& in, int layer);


Then again it looks like your reading a binary file so its not that relevant in your case.

Share this post


Link to post
Share on other sites
Having trouble getting this working.

When I pass a reference to a stream, and then use that stream in the method that receives it, will the stream know where I've got up to in the reading? Presumably the stream has some kind of counter built into it so that it knows where it has got up to. Will that counter thing get passed with the stream?

Share this post


Link to post
Share on other sites
Quote:
Original post by joebarnslondon
When I pass a reference to a stream, and then use that stream in the method that receives it, will the stream know where I've got up to in the reading? Presumably the stream has some kind of counter built into it so that it knows where it has got up to. Will that counter thing get passed with the stream?


By default a stream has an associated stream buffer (among other things that are not worth mentioning here). The stream buffer deals with the transportation layer of I/O streams architecture so yes what-ever you did previously will take effect where ever you pass it. Typically file streams are buffered I/O, you can imagine it like so:



What is the problem your having.

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!