Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

Writing an item from a struct to a file

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

I need to write a specific item from a struct to a file that already exists and need to leave the rest of the data untouched.

Here is the code

void Game::SaveGameCompleted(const string &fileName)
{
ofstream out(fileName.c_str(), ios::binary);

if(!out)
return;

GameData gameData;

GetGameData(&gameData);

out.seekp(offsetof(GameData, GameCompleted), ios::beg);
out.write((char*)&gameData.GameCompleted, sizeof(gameData.GameCompleted));

out.close();
}


It seems to be causing a crash but I can't see any obvious reason why.

Share this post


Link to post
Share on other sites
Advertisement

It seems to be causing a crash ...
[/quote]
You're not even sure?

Are you calling the function on a valid Game instance? Is the fileName parameter a valid string instance? Have you eliminated GetGameData() as the source of the problem? What is the type of GameData::GameCompleted? Which line does the crash occur on, and are there any error messages involved? If the crash is non-deterministic, say so.

You should be able to be a lot more specific about your problem.


I need to write a specific item from a struct to a file that already exists and need to leave the rest of the data untouched.
[/quote]
How much data is involved? The OS writes files in chunks called blocks, so if your file isn't significantly bigger than the size of these chunks then you should be aware that this won't save a lot of work.

Share this post


Link to post
Share on other sites
Yeah sorry about the lack of information I really just wanted to check that the syntax in the method was sound. I will check other possible problems.

Share this post


Link to post
Share on other sites
I've had more time to investigate this further and it turns out the file is being cleared before writing the data instead of overwiting. Why is this the case? From my reading you have to specify ios::trunc to clear the file before writing.

Share this post


Link to post
Share on other sites
Microsoft's documentation states:


  • ios::out The file is opened for output (implied for all ofstream objects).
  • ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified and ios::ate, ios::app, and ios:in are not specified.

    [/quote]

Share this post


Link to post
Share on other sites
Yep I got that thanks. Interesting that noone picked up on it sooner but thanks anyway. And for the record I was testing on an iPhone which isn't the easiet platform to test data output on.

Share this post


Link to post
Share on other sites
This is why minimal examples are great. Get a minimal example to work on your PC, then you'll only be dealing with iPhone specific problems (hopefully few and far between) when you port the code over. You would also have answered most of the questions I asked in my first reply by building a minimal example.

Share this post


Link to post
Share on other sites
I don't always have time to test on the PC but thanks for the advice. iPhone is a little difficult but I figured out how to download data from the documents folder. From that it was easy to examine the data file.

BTW Your Reputation is impressive

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!