What is wrong in this code and how to fix it?
int _tmain(int argc, _TCHAR* argv[])
{
std::ostream * o = &std::cout;
char text[4096];
char *file = "D://test.txt";
if ( file != NULL )
{
strcpy ( text, file );
strcat ( text, ".log" );
o = & std::ofstream ( text );
}
*o << "test"; //Exception
return 0;
}
o = & std::ofstream ( text );this creates a temporary ofstream object whose address is assigned toobut the object is instantly destroyed, soopoints to a deleted object. This should work (using static):