I'd like to use ifstream and ofstream in C++ to mimic C#'s BinaryReader / BinaryWriter functionality
问题 I'm looking for a way to write floats/ints/strings to a file and read them as floats/ints/strings. (basically read/write as ios::binary). 回答1: I subclassed ifstream and ofstream : ibfstream and obfstream . I made a little helper class that would detect the endianness of the machine I was compiling/running on. Then I added a flag for ibfstream and obfstream that indicated whether bytes in primitive types should be flipped. These classes also had methods to read/write primitive types and arrays