Hello guys, that's my first post around here and I have a problem with binary files.

I'm writing a simple editor for my arkanoid clone game. This editor opens a binary file defined by this:
0-26 byte: Descriptor for the file. Once in each file.
27->(1141*n): Levels inside the file, each level has 1141 bytes.

Here starts my problems! The game and the editor use the "Files" class, which manipulates the stream, just loading and saving levels, if in editor. Everything works fine in editor but only if i have at least a blank level file!
The game of course can't continue if there are no file levels, but the editor needs to create one, and it can't!

I need to:
- Binary file;
- Open the file to write anywhere in it without losing previous content (eg: The user may have a file with 30 levels, but want to change level 2, so not just appending);
- Creating the file if it doesn't exist (eg: User deleted the file completely or it got corrupted, which is checked elsewhere); and
- Having only one file open all time (Maybe my error is here, don't know if this is good practice or not).

Here is the code i'm using for opening the file:

file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::ate | std::ios::binary);

This is outside the constructor, the game and editor call a load default level function containing this code, and the destructor closes the file.

What I already tried:
- Playing with the open modes:

//Recreates file everytime
file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::binary);
//Can't access beginning of file; Any seek(0) returns the pointer to the end of file (after last byte)
file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::app | std::ios::binary);

- Think to create a temp file only for output -> open the levels file in input only -> copy level to temp file -> close levels file -> reopen it in output mode (erased all content) -> Copy temp file back to level file -> close temp file. Seems to much trouble, but if it's the only way...
- Trying to seek backwards with negative values (eg: seekp(-(levelSize), std::ios::cur) ) but it doesn't seem to work since it always goes forth!

Please guys, help me! I can post other parts of my code if you think it's relevant!

Thank you.

Edited by Metraton: n/a

7 Years
Discussion Span
Last Post by Metraton


The type is a bitmask type T3 that describes an object that can store the opening mode for several iostreams objects. The distinct flag values (elements) are:

* app, to seek to the end of a stream before each insertion
* ate, to seek to the end of a stream when its controlling object is first created
* binary, to read a file as a binary stream, rather than as a text stream
* in, to permit extraction from a stream
* out, to permit insertion to a stream
* trunc, to truncate an existing file when its controlling object is first created

Try with only in, out, binary.
Then seeking should work as you describe.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.