Archived

This topic is now archived and is closed to further replies.

d000hg

Problems with I/O

Recommended Posts

I need to be able to save stuff from my terrain editor and basically thi is what it does: use fprintf to put out header info, then I want to output a long sequence of short integers for heightmap data, followed by a long sequence of BYTEs. However if you use fprintf to put out shorts, you have to put a space or newline between them. I tried using fwrite to send out the data on a bit by bit copy to the file but that didn''t work. For the BYTEs I tried to use fputc, but when you put certain characters it screws things up trying to read them again. I basically want a mixture of fprintf/fscanf for bits that are human readable i.e map size etc, and a load of bytes for other stuff. What''as the best way? If you want to discuss specifics, I have a 2d array of shorts called heights, 2d array of bytes called groundtypes and header variables height & width which need to go in the file like: Width: 1234 Height: 1234 before the rest of the stuff. John 3:16

Share this post


Link to post
Share on other sites
Memory-mapped files are amazingly good for binary file i/o if you don''t mind Win32 code.

CreateFile
CreateFileMapping
MapViewOfFile

If you want to stay portable, then fwrite or ostream opened for binary access is the way to go. If you use fwrite properly, you won''t have any troubles. Perhaps you can elaborate on "that didn''t work" bit.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites