   saving std::string to a binary file?
  is there any *good* way to to this? the only way i found was to reinterpret_cast the string to a char* and save it using std::ofsream.write(...) but the loading part using std::ifstream.read(...) seems to kill my strings... 
  You''ll want to save using a length-prefixed format.This means you write out the length of the string, followed bythe actual characters.When reading back, you''ll read the length first, and know howmany characters will follow. Kami no Itte ga ore ni zettai naru! ;  At a guess: std::ostream& write_string(std::ostream& os, const std::string& str){   size_t length = str.length();   os.write((char*)&length, sizeof(size_t));   os << str;   return os;}std::istream& read_string(std::istream& is, std::string& str){   size_t length;   is.read((char*)&length, sizeof(size_t));   is.width(length);   is >> str;   return is;}Note - I''m working on a potential article on binary stream IO. I guess reading and writing strings should be part of it ;  You can write your own I/O functions like so (pseudo-code, assuming some arbitrary stream with "write" and "read" functions):To write:typedef int int32;int32 size = htonl( myString.length() );stream.write( &size, 4 );stream.write( &myString[0], size );  To read:int32 size;stream.read( &size, 4 );size = ntohl( size );myString.resize( size );stream.read( &myString[0], size );  Note that this code properly handles byte order, and is fully portable, assuming you get the typedef for int32 right on your system (hint: on any system you care about now or in the next 5 years, the typedef will be correct as-is). ;quote:Original post by hplus0603   Note that this code properly handles byte order, and is fully portable, assuming you get the typedef for int32 right on your systemOr you could just use the sizeof operator which has been in C since its creation for precisely that reason.Or you could wait until the C99 <stdint.h> header gets included in C++, giving you known-size integral types.   Good point using ntohl() though.“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 14, 2004 6:11:15 PM];quote:Original post by Fruny  At a guess:codeNote - I''m working on a potential article on binary stream IO. I guess reading and writing strings should be part of it  that should work, but the text i''m about to save, won''t be saved binary - just plain text, even though i use the std::ios_base::binary flag.. ;quote:Original post by Metus    that should work, but the text i'm about to save, won't be saved binary - just plain text, even though i use the std::ios_base::binary flag..   Typical newbie error. The binary flag doesn't mean "write in binary". It means "do not translate newlines". So long as you're using formatted IO (<< and >>) your data will be saved in whitespace-delimited text format.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 14, 2004 6:22:09 PM];  but isn''t that exactly what ofstream.write and ifstream.read are all about: binary operations?;  As for using read/write on strings, well, std::strings are not POD-types so you can''t just cast them to char*. Typically, if the size of the string is beyond a certain threshold, the string object only stores a pointer to the character buffer itself. Obviously, a cast will not find them.std::istream::read/write only works for POD-types.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  so, the best way to store strings in a binary form is to use char''s then? how does streams behave with non-null terminated strings? 