   How do I read a text file into a std::string?
#include <fstream>#include <windows.h>int WINAPI WinMain( HINSTANCE hThisInstance, HINSTANCE hPrevInstance,LPSTR lpszArgument, int nFunsterStil ){    std::ifstream xmlfile( "menu.xml" );    std::string temp;    char ch;    while( xmlfile.get(ch) ) temp += ch;Frist let me say why I want to load a text file into a std::string.I am using TinyXML to build the gui for my apps. So far I can build menus. Instead of using an xml file to load the menu I want to be able to use a string resource or other custom data format in the exe.So far I am having trouble loading a text file into a string in a way that looks clean, the only way that works so far is the while loop that I have posted in the snippet above.At first I thought it would be possible to do this:std::ifstream xmlfile( "menu.xml" );std::string temp;temp = xmlfile; // Dosent workortemp << xmlfile; // Dosent work1) Since operator << and = arent defined for a string is there a more common/correct way of doing this other than the with while loop in the source block above?2) Second TinyXML dosent seem to be able to load an xml document from a string ( which it will have to do if it comes from a resource )#include <tinyxml/tinyxml.h>TiXmlDocument  XmlAppDocument;XmlAppDocument << temp; //Dosent workis there a c++ class that I can use that is like a ifstream but actually a string? I heard about std::stringstream but that dosent seem to be working for me.thanks 
  For the first part, this is one method to copy a file to a string:  std::ifstream ifs("readme.txt");  std::string file_string;  std::copy(std::istream_iterator(ifs),  std::istream_iterator(),  std::back_inserter(file_string));  I''m not familiar with TinyXML, so I''m not sure this will be helpful, but you can initialize a std::stringstream with a std::string and it will share many of the same properties as a fstream object. ;std::ifstream ifs("readme.txt");std::string file_string( std::istreambuf_iterator<char>(ifs),std::istreambuf_iterator<char>() );   “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 4, 2004 11:11:10 PM];  Uh oh!  C++''s Most Vexing Parse strikes again!   Kami no Itte ga ore ni zettai naru! ;ifstream fin("readme.txt");ostringstream outstr;outstr << fin.rdbuf();string file_contents = outstr.str();;  Fruny dont you sleep? thank guysedit: It seems I am the one who dosent sleep  [edited by - try_catch_this on February 5, 2004 2:56:03 PM]; std::string temp( std::istreambuf_iterator(xmlfile),  std::istreambuf_iterator() );Does not compile. Apparently the compiler thinks its a function prototype. But this does:    std::ifstream xmlfile( "menu.xml" );    std::istreambuf_iterator fileInput(xmlfile);    std::istreambuf_iterator fileEnd;    std::string temp( fileInput, fileEnd ); ;  That''s what tangentz was referring to when he was talking about "C++''s most vexing parse". In theory you can also do:std::string temp( (std::istreambuf_iterator(xmlfile)),    std::istreambuf_iterator() );  (Note the extra parenthesis around the first parameter.) However, it doesn''t work on a small population of compilers. ;#include <sstream>std::stringstream stream;std::string my_string;char *the_char;// do stuff with the_char...stream<<the_char;my_string = stream.str();Dunno if thats what you want  Edit: doesn't have to be a char... My Website | Everything you need to know about 3D Graphics | Google | Search the Forums [edited by - Luke Philpot on February 5, 2004 2:22:34 AM];  Bah, you C++ elitists >_<this is the best  void load( const char* filename, std::string& str ){   int h = open( filename, O_BINARY | O_SEQUENTIAL );    int fileSize = filelength( h );    str.reserve( fileSize );    int pos = 0;int buffSize = fileSize / 4;    char* buffer = new char[buffSize];    while( pos < fileSize ){   memset( buffer, 0, buffSize );int bytes = read( h, buffer, buffSize - 1 );   str += buffer;pos += bytes;if( bytes == 0 )   break;    }    delete [] buffer;    close( h );} | C++ Debug Kit :: GameDev Kit :: DirectX Tutorials :: 2D DX Engine | TripleBuffer Software || Plug-in Manager :: System Information Class :: D3D9 Hardware Enum | DevMaster :: FlipCode |    