   Reading Binary Files
  i am working on creating save game files in binary for a game im currenlty working on. i know how to write data to a file (using ofstream and write()) but i dont understand how to read the data from the file, manipulate it, then save it back to the file. could someone please explain to me how this is accomplished?   
  Since you already know how to use ofstream/write, you can look into ifstream/read to perform the symmetric operations.   ;  To expand on that and give an example, use code like this:ifstream& MyClass::Read(ifstream &Stream) {  Stream.read(reinterpret_cast<char*> (&m_ID), sizeof(m_ID));  Stream.read(reinterpret_cast<char*> (&m_Name), sizeof(m_Name));  pm_ContainedClass->Read(Stream);  //if above is called in a constructor, e.g. MyClass::MyClass (ifstream &)  //you must remember to initialize it first, e.g.:  //Myclass::Myclass(ifstream &Stream) {  //pm_ContainedClass = new MyContainedClass(); Read(Stream);}  //done, return stream  return Stream;} // end Read(ifstream&) ofstream& MyClass::Write(ofstream &Stream) {  Stream.write(reinterpret_cast<char*> (&m_ID), sizeof(m_ID));  Stream.write(reinterpret_cast<char*> (&m_Name), sizeof(m_Name));   pm_ContainedClass->Write(Stream);  //done, return stream  return Stream;} // end Write(ofstream&)Hope this helps,-Sturla;  I like to use the old method with the FILE* I use fwrite and fread to save and get variables from my files.;Quote:Original post by sturla  Stream.write(reinterpret_cast<char*> (&m_ID), sizeof(m_ID));When we're talking about binary data, using (signed) char usually leads to all sorts of fun bugs; unsigned char is the proper representation of a byte.   ;  Using FILE is as easy as that:void Foo::Read( FILE *fp ){    fread( &an_integer, sizeof( int ), 1, fp );    fread( &m_Name, sizeof( m_Name ), 1, fp ); // This will ONLY work for a static array, that is char m_Name[ 32 ], it will not work for char *m_Name = new char[ 32 ]}Using fwrite works the same, supply a pointer to your data, state the size of 1 element and the number of elements and the file pointer.I didn't do error checking here, you might wanna do this: fwrite returns the number of bytes written to fp, so it should be equal to sizeof( element ) * num_elements.   