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.

Recommended Answers

All 2 Replies

http://docsrv.sco.com/cgi-bin/man/man?ios+3C%2B%2Bstd#ios

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.