# 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.

## 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 on other sites

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 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 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 on other sites
It looks like ofstream will clear the file by default. Changing to fstream worked.

##### 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 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 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 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.

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013559
×