function:
//http://www.cplusplus.com/reference/clibrary/ctime/strftime/
char* get_current_time()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80]; //main.cpp:73: warning: address of local variable ‘buffer’ returned
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%Y %m-%d %H-%M-%S",timeinfo);
puts (buffer);
return buffer;
}
which is invoked by:
char* filename = get_current_time();
filename = strcat(filename, ".txt");
and the puts outputs to the console:
2011 03-18 13-51-59
so… for the most part the function works…
but when I puts (filename); after the strcat, I get this:
p??_
Returning a LOCAL address is a bad bad bad idea.
In C++,
std::stringsolves most of such issues. Prefer using it.Also, forget about
strcat, and use+or/and+=withstd::string.Re-writing your code:
It looks better now.