Jump to content
  • Advertisement
Sign in to follow this  
darenking

Passing a reference to a filestream in C++ ? [SOLVED]

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

Hi all! I have a problem with passing a reference to a filestream in C++. Have searched on google and it doesn't seem to be there, just links to my own postings on message boards. _the_phantom_ kindly described the method below, but it doesn't work for me. My data file is definitely correct, have tested. It's the actual passing of the filestream that fails, as the second method just loads in random rubbish. ... I create it like this: std::ifstream file( "001-all.dat", std::ios::binary ); Load in integers from it no probs: file.read( (char*) &data, sizeof(int) ); ...but then need to send it to another method which is supposed to continue reading integers. I pass it like this: LoadMapLayer(file, MAP_LAYER_NORMALBACK); ... My next function is like this: void World::LoadMapLayer(std::ifstream &file, int layer) { file.read( (char*) &data, sizeof(int) ); } ...and just loads random rubbish! The datafile is definitely correct, have tested it by reading it all in one function. [Edited by - joebarnslondon on June 6, 2005 7:02:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
Have no idea why it might be failing, but try passing it by pointer instead of by reference. In all likelihood, it will help nothing, but when no obvious solutions are available, we must mess around [smile] Also, check if file.bad() in the second function.

Share this post


Link to post
Share on other sites
That won't fix it. You should rather try the other suggestion. Check file.fail() and file.bad().

Share this post


Link to post
Share on other sites
It means that your stream is, for all intents and purposes, good, and should not be giving you bullox results. Are you abosulutely sure you're not doing something wrong somewhere else?

Share this post


Link to post
Share on other sites
Aha!

You were right. I had an error elsewhere. See the '&' that shouldn't be in the second line:

file.read( (char*) &data, sizeof(int) );
textprintf(buffer2, font, 0, 40, 200, "width: %i.", &data );

Thanks all!

I wonder, should I delete this topic, or is it useful to leave it for other people? I'm fairly new to forums.

Share this post


Link to post
Share on other sites
Ahh, the oh-so-sweet sense of satisfaction that one gets after solving a mind-breaker... Well do I know that feeling, for I seem to get an inordinate amount of them [smile] Anyway, you might want the title of the thread, adding in '[SOLVED]' or something like that, to clue people in that if they are wishing to solve other people's problems, they can spend their time better elsewhere. In general, you wouldn't delete a thread unless it was a double (or triple) post, which this one is not.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!