Writing an item from a struct to a file

Started by
7 comments, last by Headkaze 12 years, 2 months ago
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.
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.
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.
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.
It looks like ofstream will clear the file by default. Changing to fstream worked.
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]
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.
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.
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

This topic is closed to new replies.

Advertisement