1) Where is _strdate() defined? I'm not finding it...
2) Why are you using and (which should be )?
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
You need to include time.h to be able to use _strdate() - but please note that this is a non-standard function (doesn't work in Unix).
And please use code tags.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Here is the reference site .
Here is a small example:
#include <iostream>
#include <ctime>
using namespace std ;
int main( )
{
char buffer[BUFSIZ] = { '\0' } ;
time_t now = time( &now ) ;
struct tm* local_time = new tm( ) ;
local_time = localtime( &now ) ;
strftime( buffer, BUFSIZ, "%d / %m / %Y", local_time ) ;
cout << buffer ;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
or this, which is almost identical to what ~S.O.S.~ posted. Like most things in programming there is always more than one way to do it. Unless you are instructed to do otherwise you can choose whichever method you want. One is not any better than the other unless you make the buffer too small to hold the result. In that case neither version will work correctly.
#include <iostream>
#include <ctime>
using namespace std ;
int main( )
{
char buffer[BUFSIZ] = { '\0' } ;
time_t now = time( &now ) ;
struct tm* local_time = new tm( ) ;
local_time = localtime( &now ) ;
sprintf(buffer,"%02d/%02d/%04d",
local_time->tm_mday,
local_time->tm_mon+1,
local_time->tm->year+1900);
cout << buffer ;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Isn't this leaking memory (aside from the fact that no attempt is made to delete the lost memory)?
struct tm* local_time = new tm( ) ;
local_time = localtime( &now ) ;
It is also a good habit to check return values. Time - Displaying Current
Change the format fed to strftime , as already mentioned, for the date.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Isn't this leaking memory (aside from the fact that no attempt is made to delete the lost memory)?
yes it is -- I hadn't noticed that error.:o
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343