   loading from a binary file
  ok, i am working in C++ and i want to read in data from a binary file, the data would values for a series of structs for instance the struct may look like this:struct{int position;int index;std::string image;}and the text in the file would be:235image.bmpThe number of these structs would be at the top of the file. So what i want to know is, what is the best method to read in all of this information and assign the values to their respective structs.   
// assuming the file stream has been open properly, probably would want to do extra error checking for the calls to read(), simple example thoughvoid readstruct(const std::fstream &_file, mystruct &_struct){_file.read(&_struct.position, sizeof(_struct.position));_file.read(&_struct.index, sizeof(_struct.index));int iStringLength = 0;char buffer[1024];_file.read(&iStringLength , sizeof(iStringLength));if(iStringLength > 0){_file.read(buffer, iStringLength);_struct.image = buffer;}}The only tricky part is reading in the std::string. If the struct was POD you could read the entire thing in 1 call, but you can't do that when you have dynamic members._file.read(&struct, sizeof(struct));[Edited by - DrEvil on July 9, 2006 7:45:57 PM];  The first method you listed looks like it should work, but just to confirm, the reason the secound method will not work is because the program has no way of knowing the length string to read in right?   ;  Right, the 2nd method would compile and run but you'd likely get a crash. The read function is like memcpy, it simply copies memory into the address you give it. Just as you can't memcpy into an std::string you can't read into one, or a struct that contains one, or any other dynamic structure. A decent rule of thumb is to never .read, memcpy, etc.. into any structure/object that contains pointers.If you changed the struct tostruct{int position;int index;char image[32];}Then you could read it all in 1 call, since it would then be a POD structure.   ;  hmm i tried both examples you gave but i keep getting this error, the struct name is tile:error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'tile *__w64 ' to 'char *'   with   [  _Elem=char,  _Traits=std::char_traits<char>   ]   Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castI am not sure what i am doing wrong.   ;  Can you post the code you are using? Also I added the missing assignment to the code above to set up the string. Are you coding for 64 bit?   ;  well right now all i have that i think is relevant to the problem is:void readstruct(std::istream &_file, tile &_struct){_file.read(&_struct, sizeof(_struct));}and the struct itself:struct tile{int data;char image[32];};As of now i am not calling that function anywhere. This is the code that generated the error i posted before.   ;  cast &_struct to a char *, since read takes a char *, sorry left that out of my example code.   ;  you could have read back each member if you knew the location of each block like they do in 3ds files and in md2 and bsp   ;  cast &_struct to a char *Looks like that worked, i would have tried it but I am spetical about casting.   