My opinion is that it's best to do it with the C++ standard library fstream classes. But it largely depends on what kind of data you're reading in, I suppose. Typically, if I'm the one generating the data I encapsulate it in a struct and write it out to the binary file.
That said, it seems that current attempts to accomplish what I used to be able to do easily before are mired by Microsoft's inclination to obfuscate the data type of every argument it puts in Intelisense. I used to be able to point to the data for output using a (void *). Now it seems I'm forced to use a (char *) which doesn't really make sense to me when I'm outputting binary data.
AAR, an extract from a level editor I've been working on. Mind you, the code is a part of a class and thus probably might need some revision for pure procedural use.
Outputting the level, isections is actually an array of structs.
std::ofstream levelFile;
void Grid::Save()
{
char fname [128];
strcpy (fname, fileName);
strcat (fname, ".lvl");
// TODO: add some exception handling
levelFile.open(fname, std::ios::out | std::ios::binary);
levelFile.write((const char *)isections, sizeof (isections));
levelFile.close();
}
Inputting the data into the same array:
std::ifstream levelIn;
void Grid::Load(char *infile)
{
levelIn.open(infile, std::ios::binary);
levelIn.read((char *) isections, sizeof(isections));
DrawAllStreets();
}
Also, using std:: may require making some adjustments in your project file when generating, IIRC, the program in debug mode.
Lilith, Night Butterfly
I'm not a programmer but I play one in the office