StdIO Binary Round Trip
A C++ program that writes a DWORD to a file with ofstream and reads it back with ifstream — raw binary I/O through the standard streams.
The shortest demonstration that the C++ iostream classes can do raw binary work. ofstream::write dumps the four bytes of a DWORD straight into a file (no text conversion, no newline), and ifstream::read pulls those same four bytes back out into another DWORD, which is then printed. Useful as a reference when you want portable-looking binary file handling without dropping down to Win32 CreateFile/ReadFile - for example persisting small structs or magic-number headers.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <Windows.H>
#include <StdIO.H>
#include <StdLib.H>
#include <IOStream.H>
#include <FStream.H>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
DWORD OutDword = 840823110;
DWORD InDword = 0;
//--------------------------------------------------------
ofstream FOut;
FOut.open("C:\\Test.txt");
if(FOut.good() == FALSE)
return 0;
FOut.write((char *)&OutDword, sizeof(DWORD));
FOut.close();
//--------------------------------------------------------
ifstream FIn;
FIn.open("C:\\Test.txt");
if(FIn.good() == FALSE)
return 0;
FIn.read((char *)&InDword, sizeof(DWORD));
cout << InDword << endl;
FIn.close();
//--------------------------------------------------------
return 0;
}
Two practical notes: open the streams in binary mode (std::ios::binary) so no translation layer ever touches the bytes, and remember this round trip only works between machines with the same endianness and DWORD size - for anything exchanged across platforms, serialize explicitly.